Skip to content

Instantly share code, notes, and snippets.

View mdwhatcott's full-sized avatar

Michael Whatcott mdwhatcott

View GitHub Profile
@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/testing@v0.19.0 (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 {
@mdwhatcott
mdwhatcott / bowling-test.rkt
Last active April 5, 2022 22:53
Bowling Game Kata (in Racket)
#lang racket
(require rackunit)
(require "bowling.rkt")
(define (roll-many times roll)
(for/list ([i times]) roll))
(test-equal? "Gutter Game" 0 (score (roll-many 20 0)))
(test-equal? "All Ones" 20 (score (roll-many 20 1)))
@mdwhatcott
mdwhatcott / life.clj
Last active September 13, 2021 15:12
The Game of Life Kata
(ns kata.life)
(defn neighbors-of [[x y]]
(for [Y (range (dec y) (+ 2 y))
X (range (dec x) (+ 2 x))
:when (not= [x y] [X Y])] [X Y]))
(defn count-active-neighbors [cell grid]
(->> cell neighbors-of set (filter grid) count))
@mdwhatcott
mdwhatcott / ttt.clj
Created August 24, 2021 22:28
Tic-Tac-Toe Grid GUI Prototype (clojure, quil)
(ns hello-quil.grid
(:require [quil.core :as q]
[quil.middleware :as m]))
(defn hovering? [{:keys [x y width mark] :as cell}]
(let [x2 (+ x width)
y2 (+ y width)
mx (q/mouse-x)
my (q/mouse-y)]
(and (nil? mark)
@mdwhatcott
mdwhatcott / prime-factors.clj
Created August 12, 2021 23:00
prime-factors.clj
(ns factors.core-spec
(:require [speclj.core :refer :all]
[factors.core :refer :all]))
(defn factors-of [n]
(loop [n n, d 2, fs []]
(cond (= n 1) fs
(zero? (mod n d)) (recur (/ n d) d (conj fs d))
:else (recur n (inc d) fs))))