Skip to content

Instantly share code, notes, and snippets.

View mdwhatcott's full-sized avatar

Michael Whatcott mdwhatcott

View GitHub Profile
@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))))
@mdwhatcott
mdwhatcott / framed-bowling.clj
Last active August 12, 2021 22:34
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 August 7, 2021 19:39
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) []
@mdwhatcott
mdwhatcott / bowling.clj
Created August 2, 2021 19:22
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 April 2, 2021 16:56
fmt.Sprintf -> template.Execute
package templates
import (
"bytes"
"fmt"
"io"
"text/template"
)
func ParseAndExecute(format string, args ...interface{}) string {
@mdwhatcott
mdwhatcott / bowling.rkt
Created January 16, 2021 20:06
The 'Bowling Game' Kata in Racket
#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)
@mdwhatcott
mdwhatcott / stack_trace_error.go
Created February 6, 2020 02:08
Go Stack Trace Error
type StackTraceError struct {
inner error
stack []byte
}
func NewStackTraceError(inner error) *StackTraceError {
return &StackTraceError{
inner: inner,
stack: debug.Stack(),
}