runtime_polymorphism and lazy_sequences done

This commit is contained in:
Oli Clive-Griffin 2021-06-03 20:38:58 +12:00
parent aa5f040409
commit 8d6d9b0ecf
3 changed files with 23 additions and 23 deletions

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

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 (a :name) " eats veggies."))
(defmethod diet :carnivore [a] (str (a :name) " eats animals."))
(defmethod diet :default [a] (str "I don't know what " (a :name) " 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 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 (fn [x] x) "hello"))))