Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael Whatcott mdwhatcott

View GitHub Profile
@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)
View stack_trace_error.go
type StackTraceError struct {
inner error
stack []byte
}
func NewStackTraceError(inner error) *StackTraceError {
return &StackTraceError{
inner: inner,
stack: debug.Stack(),
}
@mdwhatcott
mdwhatcott / tcr.sh
Last active Oct 5, 2019
test && commit || revert (a la Kent Beck)
View tcr.sh
#!/usr/bin/env bash
# TCR: test && commit || revert
# codified by Kent Beck
# https://medium.com/@kentbeck_7670/test-commit-revert-870bbd756864
function test() {
echo
cd `git rev-parse --show-toplevel`
go test ./...