Permalink
Browse files

A whole bunch of what I believe to be improvements.

  • Loading branch information...
1 parent 7a41a07 commit 51778060af0aced886022a9e79ddacd3e7a66c78 @Raynes committed Dec 1, 2010
Showing with 73 additions and 51 deletions.
  1. +73 −51 src/clojure_quiz/core.clj
@@ -1,59 +1,81 @@
-(ns clojure-quiz.core)
+(ns clojure-quiz.core
+ (:use [clojure.set :only [map-invert]]))
-(def target 'clojure.core)
-(def number 3)
+(def ^{:dynamic true} *target* 'clojure.core)
+(def ^{:dynamic true} *number* 100)
+(def ^{:dynamic true} *choices* 5)
(defn get-answers []
- (map
+ (into
+ {}
+ (map
(juxt (comp name key) (comp :doc meta val))
- (take number
+ (take *number*
(shuffle
- (filter (comp :doc meta val)
- (seq (ns-publics target)))))))
+ (filter (comp :doc meta val)
+ (ns-publics *target*)))))))
-(defn guess-fn-name []
- (let [answers (get-answers)
- question (rand-nth answers)]
+(defn guess-fn-name [answers]
+ (let [[k v :as question] (rand-nth (seq answers))]
(println "What is the name of this function?")
- (println (last question))
- (first question)))
+ (println v)
+ [k k]))
-(defn choose-fn-name []
- (let [answers (get-answers)
- question (rand-nth answers)
- ansmap (apply array-map (interleave (map first answers) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))]
- (println "What is the name of this function?")
- (println (last question))
- (doseq [a ansmap]
- (println (val a) ":" (key a)))
- (.toLowerCase (str (get ansmap (first question))))))
-
-(defn choose-doc []
- (let [answers (get-answers)
- question (rand-nth answers)
- ansmap (apply array-map (interleave (map last answers) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))]
- (println "What is the correct documentation for" (first question))
- (doseq [a ansmap]
- (println (val a) ":" (key a)))
- (.toLowerCase (str (get ansmap (last question))))))
-
-(defmacro print-score [right wrong questioner]
- `(do
- (println "Right:" ~right ", Wrong:" ~wrong ", Percentage:" (int (* 100 (/ ~right (+ ~right ~wrong)))))
- (recur ~right ~wrong ~questioner)))
-
-(defn quiz [right wrong questioner]
- (let [correct (questioner)
- answer (.toLowerCase (read-line))]
- (if (= correct answer)
- (do
- (println "Correct!")
- (print-score (inc right) wrong questioner))
- (do
- (println "Wrong!")
- (println "Was:" correct)
- (print-score right (inc wrong) questioner)))))
-
-(def doc-quiz (partial quiz 0 0 choose-doc))
-(def name-quiz (partial quiz 0 0 guess-fn-name))
-(def name-quiz-easy (partial quiz 0 0 choose-fn-name))
+(defn answer-map [[k v] answers]
+ (when (> (count answers) 1)
+ (apply hash-map
+ (-> (conj (take (dec *choices*) (shuffle (vals (dissoc answers k)))) v)
+ shuffle
+ (interleave "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))))
+
+(defn print-or-nil [x] (and x (println x)))
+
+(defn format-choices [header ansmap]
+ (when ansmap
+ (str header "\n"
+ (apply str (for [[ka va] (sort-by val ansmap)] (str va ": " ka "\n"))))))
+
+(defn choose-fn-name [answers]
+ (let [[k v :as answer] (rand-nth (seq answers))
+ ansmap (answer-map (reverse answer) (map-invert answers))]
+ (print-or-nil (format-choices (str "What is the name of this function?\n" v) ansmap))
+ [k (when (> (count answers) 1) (.toLowerCase (str (ansmap k))))]))
+
+(defn choose-doc [answers]
+ (let [[k v :as answer] (rand-nth (seq answers))
+ ansmap (answer-map answer answers)]
+ (print-or-nil (format-choices (str "What is the correct documentation for " k "?") ansmap))
+ [k (when (> (count answers) 1) (.toLowerCase (str (ansmap v))))]))
+
+(defn print-score [right wrong]
+ (println (str "Right: " right ", Wrong: " wrong ", Percentage: ")
+ (int (* 100 (/ right (+ right wrong)))) "\n"))
+
+(defn quiz [right wrong questioner answers]
+ (let [answers? (seq answers)
+ [rem correct] (when answers? (questioner answers))
+ answer (when correct (.toLowerCase (read-line)))]
+ (cond
+ (and (string? answer) (empty? answer))
+ (do (println "Skipping this one for now.\n")
+ (recur right wrong questioner answers))
+ (nil? correct)
+ (do (print "\nThat's all folks! Final score: ")
+ (print-score right wrong))
+ (= correct answer)
+ (do (print "\nCorrect! ")
+ (let [new-right (inc right)]
+ (print-score new-right wrong)
+ (recur new-right wrong questioner (dissoc answers rem))))
+ :else
+ (do (print (str "\nIncorrect! Was: " correct "; "))
+ (let [new-wrong (inc wrong)]
+ (print-score right new-wrong)
+ (recur right new-wrong questioner (dissoc answers rem)))))))
+
+(defn start-quiz [q]
+ (quiz 0 0 q (get-answers)))
+
+(def doc-quiz #(start-quiz #'choose-doc))
+(def name-quiz #(start-quiz #'guess-fn-name))
+(def name-quiz-easy #(start-quiz #'choose-fn-name))

0 comments on commit 5177806

Please sign in to comment.