Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Whatcott mdwhatcott

View GitHub Profile
@mdwhatcott
mdwhatcott / assert.go
Last active Jun 22, 2022
The tiniest testing library
View assert.go
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 Apr 5, 2022
Bowling Game Kata (in Racket)
View bowling-test.rkt
#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 Sep 13, 2021
The Game of Life Kata
View life.clj
(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 Aug 24, 2021
Tic-Tac-Toe Grid GUI Prototype (clojure, quil)
View ttt.clj
(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)
View 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))))
@mdwhatcott
mdwhatcott / framed-bowling.clj
Last active Aug 12, 2021
framed-bowling.clj
View framed-bowling.clj
(ns bowling.core-spec
(:require [speclj.core :refer :all]))
(defn is-strike? [[first & _]] (= 10 first))
(defn is-spare? [[first second & _]] (= 10 (+ first second)))
(defn split-frame [rolls]
(cond (is-strike? rolls) [(take 3 rolls) (rest rolls)]
(is-spare? rolls) [(take 3 rolls) (drop 2 rolls)]
:else [(take 2 rolls) (drop 2 rolls)]))
@mdwhatcott
mdwhatcott / minimal-bowling.clj
Last active Aug 7, 2021
minimal-bowling.clj
View minimal-bowling.clj
(ns bowling.core-spec
(:require [speclj.core :refer :all]))
(defn is-strike? [rolls] (= 10 (first rolls)))
(defn is-spare? [rolls] (= 10 (apply + (take 2 rolls))))
(defn ->frames [rolls]
(cond
(empty? rolls) []
View bowling.clj
(ns bowling.core-spec
(:require [speclj.core :refer :all]))
(def all-pins 10)
(def frames-per-game 10)
(defn game-over [frame] (= frame frames-per-game))
(defn strike-score [rolls] (+ all-pins (nth rolls 1) (nth rolls 2)))
(defn spare-score [rolls] (+ all-pins (nth rolls 2)))
(defn frame-score [rolls] (+ (first rolls) (second rolls)))
(defn is-strike [rolls] (= all-pins (first rolls)))
@mdwhatcott
mdwhatcott / template.go
Created Apr 2, 2021
fmt.Sprintf -> template.Execute
View template.go
package templates
import (
"bytes"
"fmt"
"io"
"text/template"
)
func ParseAndExecute(format string, args ...interface{}) string {
@mdwhatcott
mdwhatcott / bowling.rkt
Created Jan 16, 2021
The 'Bowling Game' Kata in Racket
View bowling.rkt
#lang racket
(define LAST-FRAME 10)
(define ALL-PINS 10)
(define (is-spare? rolls) (= ALL-PINS (+ (first rolls) (second rolls))))
(define (is-strike? rolls) (= ALL-PINS (first rolls)))
(define (score-frame frame score rolls)
(score-frames (add1 frame)