Skip to content

Instantly share code, notes, and snippets.

@enriclluelles
Created August 5, 2015 07:41
Show Gist options
  • Save enriclluelles/a5d46071e2695323b895 to your computer and use it in GitHub Desktop.
Save enriclluelles/a5d46071e2695323b895 to your computer and use it in GitHub Desktop.
(ns diamond.core
(:require [clojure.string :refer [join]]))
(defn char-range [start end]
(map char (range (int start) (inc (int end)))))
(defn steps
[c]
(map str (char-range \A c)))
(defn mirror
[the-seq]
(concat the-seq (->> the-seq
(reverse)
(drop 1))))
(defn diamond
"Print a diamond of letters"
[x]
(let [ch (first x)
elements (vec (steps ch))
length (count elements)
indexes (range length)
res (map (fn [el i]
(let [first-pos (- length i)
second-pos (+ length i)
spaces-between (- second-pos first-pos 1)
one (join (repeat first-pos " "))
two (if (= first-pos second-pos) "" el)
three (repeat spaces-between " ")
four el]
(concat one two three four)))
elements indexes)]
(->> res
(mirror)
(map #(join %1)))))
(defn print-diamond [x]
(doseq [r (diamond x)]
(println r)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment