Created
March 12, 2019 21:23
-
-
Save ryan-haskell/828e633b499a12139a8d982fb8293672 to your computer and use it in GitHub Desktop.
my second clojure program: a terminal-based questionnaire!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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