From 937bbfd34bc3a2c6fdbc605067cd53f71fb66252 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Fri, 19 Feb 2010 17:14:03 -0500 Subject: [PATCH] adding sequence comprehensions and a 3-argument equality --- koans/about_equalities.clj | 7 ++++-- koans/about_sequence_comprehensions.clj | 32 +++++++++++++++++++++++++ koans/path_to_enlightenment.clj | 1 + 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 koans/about_sequence_comprehensions.clj diff --git a/koans/about_equalities.clj b/koans/about_equalities.clj index fb38a63..3de964a 100644 --- a/koans/about_equalities.clj +++ b/koans/about_equalities.clj @@ -1,9 +1,12 @@ (meditations "We shall contemplate truth by testing reality, via equality." (= true false) - + "To understand reality, we must compare our expectations against reality." (= 0 (+ 1 1)) "Sometimes we will ask you to fill in the values" - (= __ (+ 1 1))) \ No newline at end of file + (= __ (+ 1 1)) + + "You can test equality of many things" + (= (+ 3 4) __ (+ 2 _))) diff --git a/koans/about_sequence_comprehensions.clj b/koans/about_sequence_comprehensions.clj new file mode 100644 index 0000000..7bcc1c8 --- /dev/null +++ b/koans/about_sequence_comprehensions.clj @@ -0,0 +1,32 @@ +(meditations + "Sequence comprehensions can bind each element in turn to a symbol" + (= __ + (for [index (range 6)] + index)) + + "They can easily emulate mapping" + (= '(0 1 4 9 16 25) + (map (fn [index] (* index index)) + (range 6)) + (for [index (range 6)] + __)) + + "And also filtering" + (= '(1 3 5 7 9) + (filter odd? (range 10)) + (for [index __ :when (odd? index)] + index)) + + "And they trivially allow combinations of the two transformations" + (= '(1 9 25 49 81) + (map (fn [index] (* index index)) + (filter odd? (range 10))) + (for [index (range 10) :when __] + __)) + + "More complex transformations can be formed with multiple binding forms" + (= [[:top :left] [:top :middle] [:top :right] + [:middle :left] [:middle :middle] [:middle :right] + [:bottom :left] [:bottom :middle] [:bottom :right]] + (for [row [:top :middle :bottom] column [:left :middle :right]] + __))) diff --git a/koans/path_to_enlightenment.clj b/koans/path_to_enlightenment.clj index 450ef35..0b7eaf6 100644 --- a/koans/path_to_enlightenment.clj +++ b/koans/path_to_enlightenment.clj @@ -20,4 +20,5 @@ (load "about_conditionals") (load "about_higher_order_functions") (load "about_runtime_polymorphism") +(load "about_sequence_comprehensions") (println "You have acheived clojure enlightenment. Namaste.")