Skip to content

Instantly share code, notes, and snippets.

@mdallastella
Last active September 4, 2017 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdallastella/bbe3fe33cfdfd537008992ee5fce806f to your computer and use it in GitHub Desktop.
Save mdallastella/bbe3fe33cfdfd537008992ee5fce806f to your computer and use it in GitHub Desktop.
(ns talking-clock
(:require [clojure.string :as str]
[clojure.pprint :refer [cl-format]]))
(defn- ->word
[n]
(cl-format nil "~r" n))
(defn- time-to-map
[time]
(let [[hours minutes] time]
{:hours (Integer/parseInt hours)
:minutes (Integer/parseInt minutes)}))
(defn- hours-to-word
[time]
(let [{hours :hours} time]
(case hours
0 (assoc time :hours-word "twelve")
12 (assoc time :hours-word "twelve")
(->> (mod hours 12)
->word
(assoc time :hours-word)))))
(defn- minutes-to-word
[time]
(let [{minutes :minutes} time]
(assoc time :minutes-word
(cond
(= 0 minutes) "o' clock"
(< minutes 10) (str "oh " (->word minutes))
:else (->word minutes)))))
(defn- time-to-str
[time]
(let [{hours :hours hours-word :hours-word minutes-word :minutes-word} time
am-pm (if (< hours 12) "am" "pm")]
(str "It's " hours-word " " minutes-word " " am-pm)))
(defn talk
[in]
(-> (str/split in #":")
time-to-map
hours-to-word
minutes-to-word
time-to-str
print))
(comment "test:"
(map (juxt identity talk)
(for [h (range 1 25)
m (range 1 60)]
(format "%02d:%02d" h m))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment