Skip to content

Instantly share code, notes, and snippets.

@polynomialspace
polynomialspace / NDjsonTojsonArr.go
Last active Mar 24, 2021
ndjson -> json array (technically golang will take '\n' as a json sep where ndjson is '\r\n' but /shrug)
View NDjsonTojsonArr.go
// NDjsonTojsonArr converts newline-delimited json (indented or minified)
// to a json array. Caller should handle cleanup on error.
func NDjsonTojsonArr(r io.Reader, w io.WriteCloser) error {
w.Write([]byte("["))
var m json.RawMessage
dec := json.NewDecoder(r)
err := dec.Decode(&m)
for {
if _, err = w.Write(m); err != nil {
@polynomialspace
polynomialspace / gobmarshal.go
Created Feb 26, 2021
similar marshal/unmarshal functions as other encoders for gob
View gobmarshal.go
func gobMarshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(v)
return buf.Bytes(), err
}
func gobUnmarshal(data []byte, v interface{}) error {
dec := gob.NewDecoder(bytes.NewReader(data))
return dec.Decode(v)
@polynomialspace
polynomialspace / TokenActor_test.go
Last active Dec 29, 2020
WIP-y POC emulating the actor model in go. This snippet is intended to be implemented later in a larger project with a specific usecase after further modification.
View TokenActor_test.go
package main
import (
"testing"
"time"
)
type TokenRequest struct {
Key string
Value string
View rMBP_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMqrj2Ea7EsGHR5v6I9Trk7K2Y/52uvuMXGUvKBkIUF4 ash@MacBook-Pro
@polynomialspace
polynomialspace / optional.go2
Created Jun 25, 2020
Attempting to do optional type stuff in Go2 and doing a terrible job at it
View optional.go2
package main
import (
"fmt"
)
type Optional(type T) struct {
p *T
}
View reverse.go
package main
import (
"fmt"
"reflect"
)
func reverse(slice interface{}) interface{} {
/* Completely inefficient but syntactically "convenient" */
s := reflect.ValueOf(slice)
View boyer-mode.go
func strStr(haystack string, needle string) int {
foundpos := -1
if len(needle) < 1 {
return 0
}
if len(needle) > len(haystack) {
return foundpos
}
/* Find unique char nearest the end of 'needle' to skip with */
View sort-characters-by-frequency.go
import (
"sort"
"strings"
)
func frequencySort(s string) string {
abcfreq := make([]struct {
chr rune
cnt int
}, 128)
@polynomialspace
polynomialspace / atou.go
Last active Mar 4, 2020
"ascii to fullwidth" convert plaintext standard chars to fullwidth
View atou.go
package main
import (
"fmt"
"os"
"bufio"
"strings"
)
func atofw(in string) (out string) {
@polynomialspace
polynomialspace / rot13.go
Last active Mar 4, 2020
rot13 / caesar implementation in go (bad/kinda silly)
View rot13.go
package main
import (
"os"
"bufio"
"fmt"
)
// double curly brackets to provide arbitrary scoping for variable shadowing.
// this is rather disgusting, but cleaner than repeat casts or more vars.