Last active
August 29, 2015 14:16
-
-
Save andrewrlee/45d67be78f1ddec18db7 to your computer and use it in GitHub Desktop.
Diamond kata without the TDD (http://claysnow.co.uk/recycling-tests-in-tdd/)
This file contains hidden or 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.core | |
| (:gen-class)) | |
| (defn letter-index [char] (- (+ 1 (int char)) (int \a))) | |
| (def letter-seq (map char (range (int \a) (+ 1 (int \z))))) | |
| (defn format-line [[char indent inner-space]] | |
| (let [whitespace (fn [i] (apply str (repeat i " "))) | |
| last-char (if (not= char \a) char "")] | |
| (str (whitespace indent) char (whitespace inner-space) last-char))) | |
| (defn letters-upto-and-including [letter] (take-while #(<= (int %) (int letter)) letter-seq)) | |
| (defn indentation-seq [letter] (reverse (range (letter-index letter)))) | |
| (def inner-space-seq (cons 0 (iterate (comp inc inc) 1))) | |
| (defn diamond-seq [char] | |
| (let [lines-to-char (map vector (letters-upto-and-including char) (indentation-seq char) inner-space-seq) | |
| all-lines (concat lines-to-char (drop 1 (reverse lines-to-char)))] | |
| (map format-line all-lines))) | |
| (defn print-diamond [char] (doseq [line (diamond-seq char)] (println line))) | |
| (defn -main | |
| [& args] | |
| (print-diamond (ffirst args))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment