Skip to content

Instantly share code, notes, and snippets.

View mdwhatcott's full-sized avatar

Michael Whatcott mdwhatcott

View GitHub Profile
@mdwhatcott
mdwhatcott / funcy.go
Created December 29, 2022 10:18
Map/Reduce/Filter (and a few friends) w/ Go generics
// Package funcy info: github.com/mdwhatcott/[email protected] (a little copy-paste is better than a little dependency)
// AUTO-GENERATED: 2022-12-29 03:18:09.371452 -0700 MST m=+0.000761384
package funcy
// FILE: funcy.go
func Complement[T any](predicate func(t T) bool) func(t T) bool {
return func(t T) bool { return !predicate(t) }
}
func Remove[T any](predicate func(t T) bool, values []T) []T {
@mdwhatcott
mdwhatcott / slogging.go
Last active December 14, 2022 16:34
Configuring a slog handler with functional options
package slogging
import (
"path/filepath"
"strings"
"golang.org/x/exp/slog"
)
type source int
@mdwhatcott
mdwhatcott / slog_shortfile.go
Created December 6, 2022 18:18
Achieving log.Lshortfile with the new slog package
// https://pkg.go.dev/golang.org/x/exp/slog
func main() {
options := slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(a slog.Attr) slog.Attr {
if a.Key != "source" {
return a
}
fields := strings.Split(a.Value.String(), ":")
@mdwhatcott
mdwhatcott / 12-bit.icls
Created November 30, 2022 04:14
12-bit color scheme for GoLand (based on https://iamkate.com/data/12-bit-rainbow/)
<scheme name="12-bit rainbow" version="142" parent_scheme="Default">
<option name="FONT_SCALE" value="1.0" />
<metaInfo>
<property name="created">2022-11-22T17:07:03</property>
<property name="ide">GoLand</property>
<property name="ideVersion">2022.2.5.0.0</property>
<property name="modified">2022-11-22T17:07:15</property>
<property name="originalScheme">12-bit rainbow</property>
</metaInfo>
<option name="CONSOLE_FONT_NAME" value="Berkeley Mono" />
@mdwhatcott
mdwhatcott / comma.go
Created November 11, 2022 17:08
My own simple, a word which hear means inefficient, implementation of humanize.Comma
package humanize
import (
"fmt"
"strings"
)
// Comma formats a number with commas separating each block of 3 numbers.
// Inspiration: https://pkg.go.dev/github.com/dustin/go-humanize#Comma
func Comma(n int) string {
@mdwhatcott
mdwhatcott / index_slice.go
Created October 26, 2022 15:33
Index a slice of things in a map
func index[K comparable, V any](list []V, key func(V) K) map[K]V {
result := make(map[K]V)
for _, value := range list {
result[key(value)] = value
}
return result
}
@mdwhatcott
mdwhatcott / get_or_set_default.go
Created October 26, 2022 04:48
Like Java's 'computeIfAbsent'...
func GetOrSetDefault[K comparable, V any](m map[K]V, key K, new_ func() V) V {
value, found := m[key]
if !found {
value = new_()
m[key] = value
}
return value
}
/*
@mdwhatcott
mdwhatcott / tiny-should.go
Last active September 7, 2022 22:45
tiny-should.go
package should
import (
"fmt"
"log"
"reflect"
"runtime"
)
type assertion func(actual any, expected ...any) error
@mdwhatcott
mdwhatcott / should.go
Last active December 28, 2022 06:57
Single-file version of: github.com/mdwhatcott/testing/should
// Package should info: github.com/mdwhatcott/[email protected] (a little copy-paste is better than a little dependency)
// AUTO-GENERATED: 2022-12-27 23:56:46.913568 -0700 MST m=+0.004900728
package should
import (
"errors"
"fmt"
"log"
"math"
"reflect"
@mdwhatcott
mdwhatcott / assert.go
Last active June 22, 2022 03:55
The tiniest testing library
package assert
import (
"reflect"
"testing"
)
// That allows assertions as in: assert.That(t, actual).Equals(expected)
func That(t *testing.T, actual interface{}) *assertion {