14-15
This commit is contained in:
parent
3c92967e74
commit
db99c57eb1
2 changed files with 24 additions and 14 deletions
|
|
@ -3,21 +3,29 @@
|
|||
|
||||
(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]
|
||||
__)
|
||||
(loop [coll coll
|
||||
reversed '()]
|
||||
(if (empty? coll)
|
||||
reversed
|
||||
(recur (rest coll) (cons (first coll) reversed)))))
|
||||
|
||||
(defn factorial [n]
|
||||
__)
|
||||
(loop [n n
|
||||
res 1]
|
||||
(if (= 0 n)
|
||||
res
|
||||
(recur (dec n) (* n res)))))
|
||||
|
||||
(meditations
|
||||
"Recursion ends with a base case"
|
||||
|
|
|
|||
|
|
@ -8,37 +8,39 @@
|
|||
|
||||
(meditations
|
||||
"Destructuring is an arbiter: it breaks up arguments"
|
||||
(= __ ((fn [[a b]] (str b a))
|
||||
[:foo :bar]))
|
||||
(= ":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")]
|
||||
__))
|
||||
(apply str (interpose " aka " (cons (str first-name " " last-name) 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"]]
|
||||
__))
|
||||
(hash-map :original-parts full-name :named-parts (hash-map :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]
|
||||
__))
|
||||
(apply str (interpose ", " (list street-address city state)))))
|
||||
|
||||
"Or more succinctly"
|
||||
(= "123 Test Lane, Testerville, TX"
|
||||
(let [{:keys [street-address __ __]} test-address]
|
||||
__))
|
||||
(let [{:keys [street-address city state]} test-address]
|
||||
(apply str (interpose ", " (list street-address city state)))))
|
||||
|
||||
"All together now!"
|
||||
(= "Test Testerson, 123 Test Lane, Testerville, TX"
|
||||
(___ ["Test" "Testerson"] test-address)))
|
||||
((fn [[a b] {:keys [street-address city state]}]
|
||||
(apply str (interpose ", " (list (str a " " b) street-address city state))))
|
||||
|
||||
["Test" "Testerson"] test-address))
|
||||
|
|
|
|||
Loading…
Reference in a new issue