conditionals done
This commit is contained in:
parent
0b3c97ce36
commit
aa5f040409
2 changed files with 21 additions and 21 deletions
|
|
@ -8,33 +8,33 @@
|
|||
|
||||
(meditations
|
||||
"Calling a function is like giving it a hug with parentheses"
|
||||
(= __ (square 9))
|
||||
(= 81 (square 9))
|
||||
|
||||
"Functions are usually defined before they are used"
|
||||
(= __ (multiply-by-ten 2))
|
||||
(= 20 (multiply-by-ten 2))
|
||||
|
||||
"But they can also be defined inline"
|
||||
(= __ ((fn [n] (* 5 n)) 2))
|
||||
(= 10 ((fn [n] (* 5 n)) 2))
|
||||
|
||||
"Or using an even shorter syntax"
|
||||
(= __ (#(* 15 %) 4))
|
||||
(= 60 (#(* 15 %) 4))
|
||||
|
||||
"Even anonymous functions may take multiple arguments"
|
||||
(= __ (#(+ %1 %2 %3) 4 5 6))
|
||||
(= 15 (#(+ %1 %2 %3) 4 5 6))
|
||||
|
||||
"Arguments can also be skipped"
|
||||
(= __ (#(str "AA" %2) "bb" "CC"))
|
||||
(= "AACC" (#(str "AA" %2) "bb" "CC"))
|
||||
|
||||
"One function can beget another"
|
||||
(= 9 (((fn [] ___)) 4 5))
|
||||
(= 9 (((fn [] +)) 4 5))
|
||||
|
||||
"Functions can also take other functions as input"
|
||||
(= 20 ((fn [f] (f 4 5))
|
||||
___))
|
||||
(= 20 ((fn [can-be-called-anything] (can-be-called-anything 4 5)) * )
|
||||
)
|
||||
|
||||
"Higher-order functions take function arguments"
|
||||
(= 25 (___
|
||||
(= 25 ((fn [f] (f 5))
|
||||
(fn [n] (* n n))))
|
||||
|
||||
"But they are often better written using the names of functions"
|
||||
(= 25 (___ square)))
|
||||
(= 25 ((fn [f] (f 5)) square)))
|
||||
|
|
|
|||
|
|
@ -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 4) :another-road-not-taken
|
||||
:else :your-road)))
|
||||
|
||||
"Or your fate may be sealed"
|
||||
(= 'doom (if-not (zero? __)
|
||||
(= 'doom (if-not (zero? 3)
|
||||
'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)))
|
||||
|
|
|
|||
Loading…
Reference in a new issue