Skip to content

Commit

Permalink
Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
1zaak committed Jun 24, 2020
1 parent 5481978 commit 12a9109
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 49 deletions.
20 changes: 10 additions & 10 deletions src/koans/07_functions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@

(meditations
"Calling a function is like giving it a hug with parentheses"
(= __ (square 9))
(= 81 (square 9))

"Functions are usually defined before they are used"
(= __ (multiply-by-ten 2))
(= 20 (multiply-by-ten 2))

"But they can also be defined inline"
(= __ ((fn [n] (* 5 n)) 2))
(= 10 ((fn [n] (* 5 n)) 2))

"Or using an even shorter syntax"
(= __ (#(* 15 %) 4))
(= 60 (#(* 15 %) 4))

"Even anonymous functions may take multiple arguments"
(= __ (#(+ %1 %2 %3) 4 5 6))
(= 15 (#(+ %1 %2 %3) 4 5 6))

"Arguments can also be skipped"
(= __ (#(str "AA" %2) "bb" "CC"))
(= "AACC" (#(str "AA" %2) "bb" "CC"))

"One function can beget another"
(= 9 (((fn [] ___)) 4 5))
(= 9 (((fn [] +)) 4 5))

"Functions can also take other functions as input"
(= 20 ((fn [f] (f 4 5))
___))
*))

"Higher-order functions take function arguments"
(= 25 (___
(= 25 ((fn [f] (f 5))
(fn [n] (* n n))))

"But they are often better written using the names of functions"
(= 25 (___ square)))
(= 25 ((fn [f] (f 5)) square)))
20 changes: 10 additions & 10 deletions src/koans/08_conditionals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@

(meditations
"You will face many decisions"
(= __ (if (false? (= 4 5))
(= :a (if (false? (= 4 5))
:a
:b))

"Some of them leave you no alternative"
(= __ (if (> 4 3)
(= [] (if (> 4 3)
[]))

"And in such a situation you may have nothing"
(= __ (if (nil? 0)
(= nil (if (nil? 0)
[:a :b :c]))

"In others your alternative may be interesting"
(= :glory (if (not (empty? ()))
:doom
__))
:glory))

"You may have a multitude of possible paths"
(let [x 5]
(= :your-road (cond (= x __) :road-not-taken
(= x __) :another-road-not-taken
:else __)))
(= :your-road (cond (= x 4) :road-not-taken
(= x 3) :another-road-not-taken
:else :your-road)))

"Or your fate may be sealed"
(= 'doom (if-not (zero? __)
(= 'doom (if-not (zero? 1)
'doom
'more-doom))

"In case of emergency, go fast"
(= "pretty fast"
(explain-exercise-velocity __))
(explain-exercise-velocity :bicycling))

"But admit it when you don't know what to do"
(= __
(= "is that even exercise?"
(explain-exercise-velocity :watching-tv)))
20 changes: 10 additions & 10 deletions src/koans/09_higher_order_functions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

(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] (< x 31)) [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] (* x 10)) (filter (fn [x] (< x 4)) [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"])))
12 changes: 6 additions & 6 deletions src/koans/10_runtime_polymorphism.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
"!")))

(defmulti diet (fn [x] (:eater x)))
(defmethod diet :herbivore [a] __)
(defmethod diet :carnivore [a] __)
(defmethod diet :default [a] __)
(defmethod diet :herbivore [a] (str (:name a) " eats veggies."))
(defmethod diet :carnivore [a] (str (:name a) " eats animals."))
(defmethod diet :default [a] (str "I don't know what " (:name a) " eats."))

(meditations
"Some functions can be used in different ways - with no arguments"
(= __ (hello))
(= "Hello World!" (hello))

"With one argument"
(= __ (hello "world"))
(= "Hello, you silly world." (hello "world"))

"Or with many arguments"
(= __
(= "Hello to this group: Peter, Paul, Mary!"
(hello "Peter" "Paul" "Mary"))

"Multimethods allow more complex dispatching"
Expand Down
14 changes: 7 additions & 7 deletions src/koans/11_lazy_sequences.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

(meditations
"There are many 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))

"Only take what you need when the sequence is large"
(= [0 1 2 3 4 5 6 7 8 9]
(take __ (range 100)))
(take 10 (range 100)))

"Or 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 8 (iterate (fn [x] (* x 2)) 1)))
(= [1 2 4 8 16 32 64 128] (take 8 (iterate (fn [x] (* x 2)) 1)))

"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 "hello")
(take 100 (iterate ___ "hello"))))
(take 100 (iterate str "hello"))))
12 changes: 6 additions & 6 deletions src/koans/12_sequence_comprehensions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

(meditations
"Sequence comprehensions can bind each element in turn to a symbol"
(= __
(= [0 1 2 3 4 5]
(for [x (range 6)]
x))

Expand All @@ -12,25 +12,25 @@
(map (fn [x] (* x x))
(range 6))
(for [x (range 6)]
__))
(* x x)))

"And also filtering"
(= '(1 3 5 7 9)
(filter odd? (range 10))
(for [x __ :when (odd? x)]
(for [x (range 10) :when (odd? x)]
x))

"Combinations of these transformations is trivial"
(= '(1 9 25 49 81)
(map (fn [x] (* x x))
(filter odd? (range 10)))
(for [x (range 10) :when __]
__))
(for [x (range 10) :when (odd? x)]
(* x x)))

"More complex transformations simply take 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]]
__)))
[row column])))

0 comments on commit 12a9109

Please sign in to comment.