Skip to content

Instantly share code, notes, and snippets.

Created November 6, 2019 10:56
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
St. Pauli TDD style Diamond Kata
(ns diamond-kata.core-test
(:require [clojure.test :refer :all]))
(defn surround
([s] (surround "_" s))
([delim s]
(str delim s delim)))
(defn indexed-alphabet [start end]
(map-indexed vector
(map char
(range (int start)
(inc (int end))))))
(defn layer [[index $char]]
(if (pos? index)
(surround $char
(nth (iterate surround "_")
(dec index)))
(str $char)))
(defn append-to-pyramid [$pyramid layer]
(conj (mapv surround $pyramid)
(defn pyramid [start end]
(reduce append-to-pyramid
(map layer
(indexed-alphabet start end))))
(defn diamond [$char]
(let [$pyramid (pyramid \a $char)]
(into $pyramid
(reverse (pop $pyramid)))))
(deftest indexed-alphabet-test
(is (= [[0 \x]] (indexed-alphabet \x \x)))
(is (= [[0 \x][1 \y]] (indexed-alphabet \x \y)))
(is (= [[0 \x][1 \y][2 \z]] (indexed-alphabet \x \z)))
(is (= [[0 \a][1 \b][2 \c][3 \d]] (indexed-alphabet \a \d))))
(deftest layer-test
(is (= "x" (layer [0 \x])))
(is (= "x_x" (layer [1 \x])))
(is (= "x___x" (layer [2 \x])))
(is (= "y_____y" (layer [3 \y]))))
(deftest pyramid-test
(is (= ["x"] (pyramid \x \x)))
(is (= ["_x_"
(pyramid \x \y)))
(is (= ["__x__"
(pyramid \x \z)))
(is (= ["___a___"
(pyramid \a \d))))
(deftest api-test
(is (= ["a"] (diamond \a)))
(is (= ["_a_"
(diamond \b)))
(is (= ["__a__"
(diamond \c)))
(is (= ["___a___"
(diamond \d))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment