Created
November 6, 2019 10:56
-
-
Save lomin/e64af7828badac1562942d888068cabc to your computer and use it in GitHub Desktop.
St. Pauli TDD style Diamond Kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
layer)) | |
(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_" | |
"y_y"] | |
(pyramid \x \y))) | |
(is (= ["__x__" | |
"_y_y_" | |
"z___z"] | |
(pyramid \x \z))) | |
(is (= ["___a___" | |
"__b_b__" | |
"_c___c_" | |
"d_____d"] | |
(pyramid \a \d)))) | |
(deftest api-test | |
(is (= ["a"] (diamond \a))) | |
(is (= ["_a_" | |
"b_b" | |
"_a_"] | |
(diamond \b))) | |
(is (= ["__a__" | |
"_b_b_" | |
"c___c" | |
"_b_b_" | |
"__a__"] | |
(diamond \c))) | |
(is (= ["___a___" | |
"__b_b__" | |
"_c___c_" | |
"d_____d" | |
"_c___c_" | |
"__b_b__" | |
"___a___"] | |
(diamond \d)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment