|
5 | 5 | [clojure.string :as s]))
|
6 | 6 |
|
7 | 7 | (def success-quotes
|
8 |
| - ["Hack and be marry!" |
| 8 | + ["Hack and be merry!" |
9 | 9 | "You're bound to be unhappy if you optimize everything. -Donald Knuth"
|
10 | 10 | "Lisp isn't a language, it's a building material. -Alan Kay"
|
11 | 11 | "The key to performance is elegance. -Jon Bentley and Doug McIlroy"
|
|
14 | 14 | (def failure-quotes
|
15 | 15 | ["If at first you don't succeed; call it version 0.1"
|
16 | 16 | "I would love to change the world, but they won't give me the source code."
|
17 |
| - "Loosing is fun. -Dwarf Fortress" |
| 17 | + "Losing is fun. -Dwarf Fortress" |
18 | 18 | "Deleted code is debugged code. -Jeff Sickel"])
|
19 | 19 |
|
20 | 20 | (defn- solve-koan [koan]
|
21 |
| - (print "and the solution is (';' as delimiter): ") |
| 21 | + (println) |
| 22 | + (print "Your solution (using \";\" as a delimiter): ") |
22 | 23 | (flush)
|
23 | 24 | (let [solutions (s/split (read-line) #";")
|
24 | 25 | filled-koan (reduce (fn [k s] (s/replace-first k #"\b___?\b" s)) koan solutions)]
|
25 | 26 | (println)
|
26 |
| - (println "Your solution:\n" filled-koan) |
| 27 | + (println "The koan, filled in with your solution:\n" filled-koan) |
27 | 28 | (if
|
28 |
| - (or (try |
29 |
| - (load-string filled-koan) |
30 |
| - (catch AssertionError e (prn e) false) |
31 |
| - (catch Exception e (prn e) false)) |
32 |
| - (= (symbol "skip-koan") (first solutions)) |
33 |
| - (= "skip-koan" (first solutions)) |
34 |
| - (= :skip-koan (first solutions))) |
35 |
| - (println "Success. " (-> success-quotes shuffle first) "\nNext koan is coming up.\n") |
| 29 | + (or (try |
| 30 | + (load-string filled-koan) |
| 31 | + (catch AssertionError e (prn e) false) |
| 32 | + (catch Exception e (prn e) false)) |
| 33 | + (= (symbol "skip-koan") (first solutions)) |
| 34 | + (= "skip-koan" (first solutions)) |
| 35 | + (= :skip-koan (first solutions))) |
| 36 | + (println "Success! " (-> success-quotes shuffle first) |
| 37 | + "\nNext koan coming right up.\n") |
36 | 38 | (do
|
37 |
| - (println "Failed." (-> failure-quotes shuffle first) "\nTry again!") |
| 39 | + (println "Oh no, failed!" (-> failure-quotes shuffle first) "\nTry again!") |
38 | 40 | (solve-koan koan)))))
|
39 | 41 |
|
40 | 42 | (defmacro random-koan [prefix-forms & forms]
|
|
43 | 45 | shuffle
|
44 | 46 | first)]
|
45 | 47 | ((fn [[doc# code#]]
|
46 |
| - (println "Solve the following koan:") |
47 |
| - (println "meditate: " doc#) |
| 48 | + (println "-----\nSolve the following koan:") |
| 49 | + (println doc#) |
48 | 50 | (println code#)
|
49 | 51 | (solve-koan (str prefix-forms code#)))
|
50 | 52 | koan)))
|
51 | 53 |
|
52 | 54 | (defn- do-run [opts]
|
53 | 55 | (let [{:keys [dojo-resource koan-resource koan-root]} opts
|
54 | 56 | koan-file (-> koan-resource
|
55 |
| - ordered-koans |
56 |
| - shuffle |
57 |
| - first) |
| 57 | + ordered-koans |
| 58 | + shuffle |
| 59 | + first) |
58 | 60 | form (s/replace (slurp (file koan-root (str koan-file ".clj")))
|
59 |
| - #"\(ns" "(comment") |
| 61 | + #"\(ns" "(comment") |
60 | 62 | form-wo-meditations (-> form
|
61 | 63 | (s/replace #"\(meditations[\s\S]*" "")
|
62 | 64 | (s/replace "\"" "\\\""))]
|
63 |
| - (println "\nmeditating uppon: " koan-file) |
64 |
| - (println "the contex of the koan:\n" form-wo-meditations) |
| 65 | + (println "\nMeditating upon: " koan-file) |
| 66 | + (println "The contex of the koan:\n" |
| 67 | + (s/trim form-wo-meditations) |
| 68 | + "\n") |
65 | 69 | (u/with-dojo [dojo-resource]
|
66 |
| - (-> (s/replace form "(meditations" (format "(random-koan \"%s\"" form-wo-meditations)) |
| 70 | + (-> (s/replace form "(meditations" (format "(random-koan \"%s\"" |
| 71 | + form-wo-meditations)) |
67 | 72 | load-string)))
|
68 | 73 | (do-run opts))
|
69 | 74 |
|
70 | 75 | (defn runner [opts]
|
71 | 76 | (println
|
72 |
| -"Showing a random koan from the project. |
73 |
| -You might never reach enlightement but you can play endlessly until you get bored and start writing your own koans!") |
| 77 | + "You might never reach enlightement, but you can play endlessly until you get bored and start writing your own koans!") |
74 | 78 | (println)
|
75 | 79 | (println
|
76 |
| -"Provide your answer or \"skip-koan\" if you want to skip the current one. You will automatically get the next random koan on success. Use ';' as delimiter if you need to provide multiple answers. If you need to provide multiple answers but only some of those have effect on the given koan you have to provide your solution in the right place; however, what you type for the rest is not important, you can keep them blank or use :skip.") |
| 80 | + "Provide your answer or \"skip-koan\" if you want to skip the current one. You will automatically get the next random koan on success. Use \";\" as delimiter if you need to provide multiple answers. If you need to provide multiple answers but only some of those have effect on the given koan you have to provide your solution in the right place; however, what you type for the rest is not important, you can keep them blank or use :skip.") |
| 81 | + (println "-----") |
77 | 82 | (do-run opts))
|
0 commit comments