Skip to content

Instantly share code, notes, and snippets.

@ryan-haskell
Created March 12, 2019 21:23
Show Gist options
  • Save ryan-haskell/828e633b499a12139a8d982fb8293672 to your computer and use it in GitHub Desktop.
Save ryan-haskell/828e633b499a12139a8d982fb8293672 to your computer and use it in GitHub Desktop.
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