Skip to content

Commit

Permalink
Day 4
Browse files Browse the repository at this point in the history
  • Loading branch information
1zaak committed Jun 25, 2020
1 parent 12a9109 commit 026632e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 8 additions & 8 deletions src/koans/13_creating_functions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 :c :d]
(let [ab-adder (partial concat [:a :b])]
(ab-adder [__ __])))
(ab-adder [:c :d])))

"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))))
10 changes: 5 additions & 5 deletions src/koans/14_recursion.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

(defn is-even? [n]
(if (= n 0)
__
(___ (is-even? (dec n)))))
true
(not (is-even? (dec n)))))

(defn is-even-bigint? [n]
(loop [n n
acc true]
(if (= n 0)
__
false
(recur (dec n) (not acc)))))

(defn recursive-reverse [coll]
__)
(reverse coll))

(defn factorial [n]
__)
(reduce * (range (bigint 1) (inc (bigint n)))))

(meditations
"Recursion ends with a base case"
Expand Down
14 changes: 7 additions & 7 deletions src/koans/15_destructuring.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@

(meditations
"Destructuring is an arbiter: it breaks up arguments"
(= __ ((fn [[a b]] (str b a))
(= ":bar:foo" ((fn [[a b]] (str b a))
[:foo :bar]))

"Whether in function definitions"
(= (str "An Oxford comma list of apples, "
"oranges, "
"and pears.")
((fn [[a b c]] __)
((fn [[a b c]] (str "An Oxford comma list of " a ", " b ", and " c "." ))
["apples" "oranges" "pears"]))

"Or in let expressions"
(= "Rich Hickey aka The Clojurer aka Go Time aka Lambda Guru"
(let [[first-name last-name & aliases]
(list "Rich" "Hickey" "The Clojurer" "Go Time" "Lambda Guru")]
__))
(str first-name " " last-name " aka " (apply str (interpose " aka " aliases)))))

"You can regain the full argument if you like arguing"
(= {:original-parts ["Stephen" "Hawking"] :named-parts {:first "Stephen" :last "Hawking"}}
(let [[first-name last-name :as full-name] ["Stephen" "Hawking"]]
__))
{:original-parts full-name :named-parts {:first first-name :last last-name}}))

"Break up maps by key"
(= "123 Test Lane, Testerville, TX"
(let [{street-address :street-address, city :city, state :state} test-address]
__))
(str (get test-address :street-address) ", " (get test-address :city) ", " (get test-address :state))))

"Or more succinctly"
(= "123 Test Lane, Testerville, TX"
(let [{:keys [street-address __ __]} test-address]
__))
(let [{:keys [street-address city state]} test-address]
(str street-address ", " city ", " state)))

"All together now!"
(= "Test Testerson, 123 Test Lane, Testerville, TX"
Expand Down

0 comments on commit 026632e

Please sign in to comment.