2010-07-18 16:25:53 +00:00
|
|
|
(ns path-to-answer-sheet
|
|
|
|
|
(:use [path-to-enlightenment :exclude (run)]
|
2010-11-09 00:07:59 +00:00
|
|
|
[clojure.string :only (join split trim)]))
|
2010-07-18 16:25:53 +00:00
|
|
|
|
|
|
|
|
(def answers
|
|
|
|
|
{"equalities" {"__" [true
|
|
|
|
|
2
|
|
|
|
|
7
|
2010-10-29 15:18:09 +00:00
|
|
|
5
|
|
|
|
|
4/2
|
|
|
|
|
false
|
2010-11-04 13:01:34 +00:00
|
|
|
6/3
|
|
|
|
|
3]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2011-02-03 18:00:03 +00:00
|
|
|
"lists" {"__" [1 2 3 4 5
|
2010-07-18 16:25:53 +00:00
|
|
|
1
|
|
|
|
|
[2 3 4 5]
|
2011-02-03 18:00:03 +00:00
|
|
|
()
|
2010-07-18 16:25:53 +00:00
|
|
|
[:a :b :c :d :e]
|
|
|
|
|
[0 :a :b :c :d :e]
|
|
|
|
|
:a
|
|
|
|
|
[:b :c :d :e]
|
2011-02-03 18:00:03 +00:00
|
|
|
"No dice!"
|
|
|
|
|
()]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-10-29 15:45:47 +00:00
|
|
|
"vectors" {"__" [1
|
2010-10-27 05:04:32 +00:00
|
|
|
[]
|
|
|
|
|
[1]
|
|
|
|
|
[nil]
|
2010-07-18 16:25:53 +00:00
|
|
|
2
|
|
|
|
|
[333]
|
2010-10-27 05:04:32 +00:00
|
|
|
:peanut
|
2010-07-18 16:25:53 +00:00
|
|
|
:jelly
|
|
|
|
|
:jelly
|
2011-02-08 21:57:15 +00:00
|
|
|
[:butter :and]
|
|
|
|
|
3]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-10-29 15:45:47 +00:00
|
|
|
"sets" {"__" [3
|
2010-10-27 22:30:34 +00:00
|
|
|
#{1 2 3 4 5}
|
2010-07-18 16:25:53 +00:00
|
|
|
#{1 2 3 4 5}
|
|
|
|
|
#{2 3}
|
|
|
|
|
#{1 4}]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-07-18 16:25:53 +00:00
|
|
|
"maps" {"__" [0
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
2
|
|
|
|
|
1
|
|
|
|
|
1
|
2011-02-03 18:00:03 +00:00
|
|
|
"Vancouver"
|
2011-02-03 18:26:58 +00:00
|
|
|
nil
|
2011-02-03 18:00:03 +00:00
|
|
|
:key-not-found
|
2010-07-18 16:25:53 +00:00
|
|
|
true
|
|
|
|
|
false
|
2011-02-03 18:00:03 +00:00
|
|
|
"February"
|
|
|
|
|
1 "January"
|
2010-07-18 16:25:53 +00:00
|
|
|
2006 2010 2014
|
2011-02-03 18:00:03 +00:00
|
|
|
"Vancouver"]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-10-29 15:07:11 +00:00
|
|
|
"functions" {"__" [20
|
|
|
|
|
10 5
|
|
|
|
|
30 2
|
2010-11-05 22:33:06 +00:00
|
|
|
15
|
2010-11-07 16:17:58 +00:00
|
|
|
20 '*]
|
2010-11-04 13:10:24 +00:00
|
|
|
"___" ['(fn [f] (f 5))
|
2011-02-03 17:16:47 +00:00
|
|
|
'(fn [f] (f 5))]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-07-18 16:25:53 +00:00
|
|
|
"conditionals" {"__" [:a
|
|
|
|
|
[]
|
|
|
|
|
0
|
|
|
|
|
:glory
|
2010-11-04 01:38:20 +00:00
|
|
|
4 6 :your_road
|
2011-04-29 20:34:17 +00:00
|
|
|
''doom 0
|
|
|
|
|
:cocked-pistol
|
|
|
|
|
:say-what?]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-07-18 16:25:53 +00:00
|
|
|
"higher_order_functions" {"__" [4 8 12
|
2010-11-04 13:10:24 +00:00
|
|
|
'(* x x)
|
2010-07-18 16:25:53 +00:00
|
|
|
[false false true false false]
|
2011-02-03 18:00:03 +00:00
|
|
|
()
|
2010-07-18 16:25:53 +00:00
|
|
|
true
|
2010-11-04 13:10:24 +00:00
|
|
|
'(< x 31)
|
|
|
|
|
'(* 10 x) '(< x 4)
|
2010-07-18 16:25:53 +00:00
|
|
|
24
|
|
|
|
|
100
|
2010-11-04 13:10:24 +00:00
|
|
|
'(count a) '(count b)]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-11-04 13:10:24 +00:00
|
|
|
"runtime_polymorphism" {"__" ['(str (:name a) " eats veggies.")
|
|
|
|
|
'(str (:name a) " eats animals.")
|
|
|
|
|
'(str "I don't know what " (:name a) " eats.")
|
2011-02-03 18:00:03 +00:00
|
|
|
"Hello World!"
|
|
|
|
|
"Hello, you silly world."
|
|
|
|
|
"Hello to this group: Peter, Paul, Mary!" ]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2011-03-31 13:11:27 +00:00
|
|
|
"lazy_sequences" {"__" [[1 2 3 4]
|
|
|
|
|
[0 1 2 3 4]
|
|
|
|
|
10
|
|
|
|
|
95
|
|
|
|
|
'(range 20)
|
|
|
|
|
:a]
|
|
|
|
|
"___" ['(fn [x] :foo)]}
|
|
|
|
|
|
2010-07-18 16:25:53 +00:00
|
|
|
"sequence_comprehensions" {"__" [[0 1 2 3 4 5]
|
2010-11-04 13:10:24 +00:00
|
|
|
'(* index index)
|
|
|
|
|
'(range 10)
|
|
|
|
|
'(odd? index) '(* index index)
|
|
|
|
|
'[row column]
|
2010-07-18 16:25:53 +00:00
|
|
|
]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2010-11-17 04:23:33 +00:00
|
|
|
"creating_functions" {"__" [true false true
|
|
|
|
|
4
|
2010-07-18 16:25:53 +00:00
|
|
|
:a :b :c :d
|
2010-11-17 04:23:33 +00:00
|
|
|
:c :d
|
|
|
|
|
4
|
|
|
|
|
8]
|
|
|
|
|
"___" ['(complement nil?)
|
2011-02-03 18:00:03 +00:00
|
|
|
'multiply-by-5
|
2010-11-17 04:23:33 +00:00
|
|
|
'(comp dec square)]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2011-03-31 22:36:49 +00:00
|
|
|
"recursion" {"__" [true
|
|
|
|
|
'acc
|
|
|
|
|
'(loop [coll coll
|
|
|
|
|
acc ()]
|
|
|
|
|
(if (seq coll)
|
|
|
|
|
(recur (rest coll) (conj acc (first coll)))
|
|
|
|
|
acc))
|
|
|
|
|
'(loop [n n
|
|
|
|
|
acc 1]
|
2010-11-03 03:55:34 +00:00
|
|
|
(if (zero? n)
|
|
|
|
|
acc
|
2010-11-07 16:17:58 +00:00
|
|
|
(recur (dec n) (* acc n))))]
|
|
|
|
|
"___" ['not]}
|
2011-02-14 01:53:25 +00:00
|
|
|
|
2011-02-03 18:00:03 +00:00
|
|
|
"destructuring" {"__" [":bar:foo"
|
2010-11-09 00:07:59 +00:00
|
|
|
'(format (str "First comes %s, "
|
|
|
|
|
"then comes %s, "
|
|
|
|
|
"then comes %s with the baby carriage")
|
|
|
|
|
a b c)
|
2011-02-14 01:21:15 +00:00
|
|
|
'(apply str
|
2011-02-14 01:53:25 +00:00
|
|
|
(interpose " "
|
|
|
|
|
(apply list
|
|
|
|
|
first-name
|
|
|
|
|
last-name
|
|
|
|
|
(interleave (repeat "aka") aliases))))
|
2010-11-09 00:07:59 +00:00
|
|
|
'{:original-parts full-name
|
2010-11-12 00:16:55 +00:00
|
|
|
:named-parts {:first first-name :last last-name}}
|
|
|
|
|
'(str street-address ", " city ", " state)
|
|
|
|
|
'city 'state
|
|
|
|
|
'(str street-address ", " city ", " state)]
|
|
|
|
|
"___" ['(fn [[fname lname]
|
|
|
|
|
{:keys [street-address city state]}]
|
|
|
|
|
(str fname " " lname ", "
|
|
|
|
|
street-address ", " city ", " state))
|
|
|
|
|
]}
|
2011-02-03 18:00:03 +00:00
|
|
|
"refs" {"__" ["hello"
|
|
|
|
|
"hello"
|
|
|
|
|
"better"
|
|
|
|
|
"better!!!"
|
2010-11-11 01:16:50 +00:00
|
|
|
'(dosync (ref-set the-world 0))
|
2011-03-31 12:24:09 +00:00
|
|
|
'(map :jerry [@the-world @bizarro-world])
|
2010-11-10 02:19:10 +00:00
|
|
|
]
|
2011-03-31 12:24:09 +00:00
|
|
|
"___" ['(fn [x] (+ 20 x))]}
|
2011-02-08 11:51:11 +00:00
|
|
|
|
2011-03-31 12:44:54 +00:00
|
|
|
"atoms" {"__" [0
|
|
|
|
|
1
|
|
|
|
|
'(swap! atomic-clock (partial + 4))
|
|
|
|
|
20
|
|
|
|
|
20
|
|
|
|
|
'atomic-clock 20 :fin
|
|
|
|
|
]}
|
|
|
|
|
|
2011-02-14 01:53:25 +00:00
|
|
|
"macros" {"__" ['~(first form)
|
|
|
|
|
'~(nth form 2)
|
|
|
|
|
'form
|
|
|
|
|
'(drop 2 form)
|
|
|
|
|
"Hello, Macros!"
|
|
|
|
|
10
|
|
|
|
|
''(+ 9 1)
|
|
|
|
|
''(* 10 2)
|
|
|
|
|
''(+ 10 (2 * 3))]}
|
|
|
|
|
|
|
|
|
|
"datatypes" {"__" ['(print
|
|
|
|
|
(str "You're really the "
|
|
|
|
|
(.category this)
|
|
|
|
|
", " recipient "... sorry."))
|
|
|
|
|
"peace"
|
|
|
|
|
"literature"
|
|
|
|
|
"physics"
|
|
|
|
|
nil
|
|
|
|
|
[true false]
|
|
|
|
|
(str "Congratulations on your Best Picture Oscar, "
|
|
|
|
|
"Evil Alien Conquerors!")]}})
|
2010-07-18 16:25:53 +00:00
|
|
|
|
|
|
|
|
(defn replace-with [s k replacements]
|
|
|
|
|
(let [unreplaced-texts (split s (re-pattern (str "\\b" k "\\b")))]
|
|
|
|
|
(join
|
|
|
|
|
(butlast
|
|
|
|
|
(interleave
|
|
|
|
|
unreplaced-texts
|
2011-02-03 18:00:03 +00:00
|
|
|
(concat (map pr-str replacements) (repeat k)))))))
|
2010-07-18 16:25:53 +00:00
|
|
|
|
|
|
|
|
(defn koan-text [koan]
|
|
|
|
|
(slurp (str "src/koans/" koan ".clj")))
|
|
|
|
|
|
|
|
|
|
(defn answers-for [koan sym]
|
|
|
|
|
((answers koan {}) sym []))
|
|
|
|
|
|
|
|
|
|
(defn fill-in-answers [text koan sym]
|
|
|
|
|
(replace-with text sym (answers-for koan sym)))
|
|
|
|
|
|
2011-02-08 22:07:36 +00:00
|
|
|
(defn print-non-failing-error [koan]
|
|
|
|
|
(println (str "\n" koan ".clj is passing without filling in the blanks")))
|
2011-02-08 21:51:05 +00:00
|
|
|
|
2011-02-08 22:07:36 +00:00
|
|
|
(defn ensure-failing-without-answers []
|
2011-04-30 03:54:49 +00:00
|
|
|
(binding [clojure.test/*test-out*
|
2011-02-08 22:07:36 +00:00
|
|
|
(java.io.PrintWriter. (java.io.ByteArrayOutputStream.))]
|
2011-04-30 03:54:49 +00:00
|
|
|
(if (every?
|
|
|
|
|
(fn [koan]
|
|
|
|
|
(let [form (koan-text koan)
|
|
|
|
|
result (load-string form)]
|
|
|
|
|
(if result
|
|
|
|
|
(print-non-failing-error koan)
|
|
|
|
|
:pass)))
|
|
|
|
|
ordered-koans)
|
|
|
|
|
(println "\nTests all fail before the answers are filled in."))))
|
2010-07-18 16:25:53 +00:00
|
|
|
|
2011-02-08 22:07:36 +00:00
|
|
|
(defn ensure-passing-with-answers []
|
|
|
|
|
(try
|
|
|
|
|
(dorun (map
|
|
|
|
|
(fn [koan]
|
|
|
|
|
(load-string
|
|
|
|
|
(-> (koan-text koan)
|
|
|
|
|
(fill-in-answers koan "__")
|
|
|
|
|
(fill-in-answers koan "___"))))
|
|
|
|
|
ordered-koans))
|
2011-04-30 03:54:49 +00:00
|
|
|
(println "\nAll tests pass after the answers are filled in.")
|
2010-07-18 16:25:53 +00:00
|
|
|
|
|
|
|
|
(catch Exception e
|
|
|
|
|
(println "\nAnswer sheet fail: " e)
|
|
|
|
|
(.printStackTrace e)
|
|
|
|
|
(println "Answer sheet fail"))))
|
|
|
|
|
|
2011-02-08 22:07:36 +00:00
|
|
|
(defn run []
|
|
|
|
|
(ensure-failing-without-answers)
|
|
|
|
|
(ensure-passing-with-answers))
|
|
|
|
|
|
|
|
|
|
|