Skip to content

Instantly share code, notes, and snippets.

@geofflane
Created April 18, 2012 17:29
Show Gist options
  • Save geofflane/2415277 to your computer and use it in GitHub Desktop.
Save geofflane/2415277 to your computer and use it in GitHub Desktop.
Berlin Clock Kata in clojure
(import '(java.text SimpleDateFormat))
(defn make-counts [time]
^{:doc "*make-counts* deconstructs a time into the 5 integer
values needed by the berlin clock for each line"
}
[seq
(mod (.getSeconds time) 2)
(unchecked-divide-int (.getHours time) 5)
(mod (.getHours time) 5)
(unchecked-divide-int (.getMinutes time) 5)
(mod (.getMinutes time) 5)
])
(defn make-line [active total]
^{:doc "*make-line* creates a single line with active number of
enabled \"lights\" and the remaining total inactive"
}
(clojure.string/join
(concat (repeat active "*") (repeat (- total active) "."))))
(defn make-lines [counts]
^{:doc "*make-lines* creates all 5 lines of the berlin clock
returning them as a seq of strings"
}
[
(make-line (nth counts 1) 1)
(make-line (nth counts 2) 4)
(make-line (nth counts 3) 4)
(make-line (nth counts 4) 11)
(make-line (nth counts 5) 4)]
)
(defn berlin-clock-print [time]
^{:doc "*berlin-clock-print* constructs the complete
berlin clock for the given time"
}
(clojure.string/join "\n" (make-lines (make-counts time)))
)
; Using Java classes, not sure how to do this with plain-old-clojure
(defn parse-time [time-str]
^{:doc "*parse-time* parses a time in the format of HH:mm:ss
into a java.util.Date"
}
(.parse (SimpleDateFormat. "HH:mm:ss") time-str))
(println (berlin-clock-print (parse-time "21:22:05")))
(println (berlin-clock-print (parse-time "01:15:02")))
(println (berlin-clock-print (parse-time "01:00:01")))
(println (berlin-clock-print (parse-time "12:00:00")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment