koans 8-12

This commit is contained in:
devmeyster 2016-04-10 20:25:56 -07:00
parent 995ebd7073
commit 3c92967e74
6 changed files with 47 additions and 47 deletions

View file

@ -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 3) :road-not-taken
(= x 2) :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)))

View file

@ -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] (* 10 x)) (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"])))

View file

@ -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"

View file

@ -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 20 (iterate inc 0)))
(= '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19) (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 :hello)
(take 100 (iterate ___ :hello))))
(take 100 (iterate identity :hello))))

View file

@ -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))
@ -12,20 +12,20 @@
(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]
@ -33,4 +33,4 @@
[:bottom :left] [:bottom :middle] [:bottom :right]]
(for [row [:top :middle :bottom]
column [:left :middle :right]]
__)))
[row column])))

View file

@ -5,31 +5,31 @@
(meditations
"One may know what they seek by knowing what they do not seek"
(= [__ __ __] (let [not-a-symbol? (complement symbol?)]
(= [true false true] (let [not-a-symbol? (complement symbol?)]
(map not-a-symbol? [:a 'b "c"])))
"Praise and 'complement' may help you separate the wheat from the chaff"
(= [:wheat "wheat" 'wheat]
(let [not-nil? ___]
(let [not-nil? (complement nil?)]
(filter not-nil? [nil :wheat nil "wheat" nil 'wheat nil])))
"Partial functions allow procrastination"
(= 20 (let [multiply-by-5 (partial * 5)]
(___ __)))
(multiply-by-5 4)))
"Don't forget: first things first"
(= [__ __ __ __]
(= [:a :b 1 2]
(let [ab-adder (partial concat [:a :b])]
(ab-adder [__ __])))
(ab-adder [1 2])))
"Functions can join forces as one 'composed' function"
(= 25 (let [inc-and-square (comp square inc)]
(inc-and-square __)))
(inc-and-square 4)))
"Have a go on a double dec-er"
(= __ (let [double-dec (comp dec dec)]
(= 8 (let [double-dec (comp dec dec)]
(double-dec 10)))
"Be careful about the order in which you mix your functions"
(= 99 (let [square-and-dec ___]
(= 99 (let [square-and-dec (comp dec square)]
(square-and-dec 10))))