forked from functional-koans/clojure-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
168 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
(meditations | ||
"We shall contemplate truth by testing reality, via equality." | ||
(= __ true) | ||
(= true true) | ||
|
||
"To understand reality, we must compare our expectations against reality." | ||
(= __ (+ 1 1)) | ||
(= 2 (+ 1 1)) | ||
|
||
"You can test equality of many things" | ||
(= (+ 3 4) __ (+ 2 __)) | ||
(= (+ 3 4) 7 (+ 2 5)) | ||
|
||
"Some things may appear different, but be the same" | ||
(= 2 2/1 __) | ||
(= 2 2/1 4/2) | ||
|
||
"You cannot generally float to heavens of integers" | ||
(= __ (= 2 2.0)) | ||
(= false (= 2 2.0)) | ||
|
||
"But a looser equality is also possible" | ||
(== 2.0 2 __) | ||
(== 2.0 2 2/1) | ||
|
||
"When things cannot be equal, they must be different" | ||
(not= :fill-in-the-blank __)) | ||
(not= :fill-in-the-blank :blah)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
(meditations | ||
"The map function relates a sequence to another" | ||
(= [__ __ __] (map (fn [x] (* 4 x)) [1 2 3])) | ||
(= [4 8 12] (map (fn [x] (* 4 x)) [1 2 3])) | ||
|
||
"You may create that mapping" | ||
(= [1 4 9 16 25] (map (fn [x] __) [1 2 3 4 5])) | ||
(= [1 4 9 16 25] (map (fn [x] (* x x)) [1 2 3 4 5])) | ||
|
||
"Or use the names of existing functions" | ||
(= __ (map nil? [:a :b nil :c :d])) | ||
(= [false false true false false] (map nil? [:a :b nil :c :d])) | ||
|
||
"A filter can be strong" | ||
(= __ (filter (fn [x] false) '(:anything :goes :here))) | ||
(= '() (filter (fn [x] false) '(:anything :goes :here))) | ||
|
||
"Or very weak" | ||
(= __ (filter (fn [x] true) '(:anything :goes :here))) | ||
(= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here))) | ||
|
||
"Or somewhere in between" | ||
(= [10 20 30] (filter (fn [x] __) [10 20 30 40 50 60 70 80])) | ||
(= [10 20 30] (filter (fn [x] (>= 30 x)) [10 20 30 40 50 60 70 80])) | ||
|
||
"Maps and filters may be combined" | ||
(= [10 20 30] (map (fn [x] __) (filter (fn [x] __) [1 2 3 4 5 6 7 8]))) | ||
(= [10 20 30] (map (fn [x] (* 10 x)) (filter (fn [x] (>= 3 x)) [1 2 3 4 5 6 7 8]))) | ||
|
||
"Reducing can increase the result" | ||
(= __ (reduce (fn [a b] (* a b)) [1 2 3 4])) | ||
(= 24 (reduce (fn [a b] (* a b)) [1 2 3 4])) | ||
|
||
"You can start somewhere else" | ||
(= 2400 (reduce (fn [a b] (* a b)) __ [1 2 3 4])) | ||
(= 2400 (reduce (fn [a b] (* a b)) 100 [1 2 3 4])) | ||
|
||
"Numbers are not the only things one can reduce" | ||
(= "longest" (reduce (fn [a b] | ||
(if (< __ __) b a)) | ||
(if (< (count a) (count b)) b a)) | ||
["which" "word" "is" "longest"]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
(meditations | ||
"You may have done more with Java than you know" | ||
(= __ (class "warfare")) ; hint: try typing (javadoc "warfare") in the REPL | ||
(= String (class "warfare")) ; hint: try typing (javadoc "warfare") in the REPL | ||
|
||
"The dot signifies easy and direct Java interoperation" | ||
(= __ (.toUpperCase "select * from")) | ||
(= "SELECT * FROM" (.toUpperCase "select * from")) | ||
|
||
"But instance method calls are very different from normal functions" | ||
(= ["SELECT" "FROM" "WHERE"] (map ___ ["select" "from" "where"])) | ||
(= ["SELECT" "FROM" "WHERE"] (map #(.toUpperCase %) ["select" "from" "where"])) | ||
|
||
"Constructing might be harder than breaking" | ||
(= 10 (let [latch (java.util.concurrent.CountDownLatch. __)] | ||
(= 10 (let [latch (java.util.concurrent.CountDownLatch. 10)] | ||
(.getCount latch))) | ||
|
||
"Static methods are slashing prices!" | ||
(== __ (Math/pow 2 10))) | ||
(== 1024 (Math/pow 2 10))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
(meditations | ||
"There are a wide range of ways to generate a sequence" | ||
(= __ (range 1 5)) | ||
(= [1 2 3 4] (range 1 5)) | ||
|
||
"The range starts at the beginning by default" | ||
(= __ (range 5)) | ||
(= [0 1 2 3 4] (range 5)) | ||
|
||
"It's important to only take what you need from a big sequence" | ||
(= [0 1 2 3 4 5 6 7 8 9] | ||
(take __ (range 100))) | ||
(take 10 (range 100))) | ||
|
||
"You can also limit results by dropping what you don't need" | ||
(= [95 96 97 98 99] | ||
(drop __ (range 100))) | ||
(drop 95 (range 100))) | ||
|
||
"Iteration provides an infinite lazy sequence" | ||
(= __ (take 20 (iterate inc 0))) | ||
(= (range 20) (take 20 (iterate inc 0))) | ||
|
||
"Repetition is key" | ||
(= [:a :a :a :a :a :a :a :a :a :a ] | ||
(repeat 10 __)) | ||
(repeat 10 :a)) | ||
|
||
"Iteration can be used for repetition" | ||
(= (repeat 100 :foo) | ||
(take 100 (iterate ___ :foo)))) | ||
(take 100 (iterate (fn [x] :foo) :foo)))) |
Oops, something went wrong.