From 026632ef85456850d27642335aa9dca8ac09517b Mon Sep 17 00:00:00 2001 From: izaak Date: Fri, 26 Jun 2020 02:23:45 +0800 Subject: [PATCH] Day 4 --- src/koans/13_creating_functions.clj | 16 ++++++++-------- src/koans/14_recursion.clj | 10 +++++----- src/koans/15_destructuring.clj | 14 +++++++------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/koans/13_creating_functions.clj b/src/koans/13_creating_functions.clj index 7d84bc8d1..35910bd33 100644 --- a/src/koans/13_creating_functions.clj +++ b/src/koans/13_creating_functions.clj @@ -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)))) diff --git a/src/koans/14_recursion.clj b/src/koans/14_recursion.clj index ca2978439..a8a564bac 100644 --- a/src/koans/14_recursion.clj +++ b/src/koans/14_recursion.clj @@ -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" diff --git a/src/koans/15_destructuring.clj b/src/koans/15_destructuring.clj index 32fc9831b..889cc1e31 100644 --- a/src/koans/15_destructuring.clj +++ b/src/koans/15_destructuring.clj @@ -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"