Skip to content

Instantly share code, notes, and snippets.

@Morantron
Created August 7, 2015 09:17
Show Gist options
  • Save Morantron/579d22781f09effbf3e0 to your computer and use it in GitHub Desktop.
Save Morantron/579d22781f09effbf3e0 to your computer and use it in GitHub Desktop.
Print diamond kata
(ns clojure-dojo.core)
(use '[clojure.string :only (join split)])
(defn letter-index
[letter]
(- (int letter) 64))
(defn letter-pairs
[letter]
(vec (map-indexed (fn [i a]
{
:letter (char a)
:margin (- (letter-index letter) (inc i))
:padding (- (* 2 i) 1)
})
(range (int \A) (inc (int letter))))))
(defn print-times
[times string]
(join (repeat times string)))
(defn print-diamond
([letter]
(loop
[[pair & pairs] (concat (letter-pairs letter) (reverse (pop (letter-pairs letter))))]
(when (not (empty? pair))
(println (str
(print-times (:margin pair) " ")
(:letter pair)
(if (> (:padding pair) 0)
(str (print-times (:padding pair) " ") (:letter pair))
)
))
(recur pairs)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment