Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
my second clojure program: a terminal-based questionnaire!
(ns questionnaire)
(def questions
{ "What's your name?" :name
"What's your favorite color?" :color
})
(defn bold [text] (str "\033[1m" text "\033[0m"))
(defn prompt [question] (do (print (str (bold question) " ")) (flush) (read-line)))
(defn pairs [obj] (map (fn [key] { :key key, :value (obj key)}) (keys obj)))
(defn answer
([questions]
(answer questions {}))
([questions answers]
(if (not (= {} questions))
(let
[ current (first (pairs questions))
key (current :key)
value (current :value)
remaining (dissoc questions key)
response (prompt key)
]
(answer remaining (assoc answers value response))
)
answers)))
(defn main [] (answer questions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment