Take pity and clarify higher-order fns a bit

This commit is contained in:
Micah Martin and Colin Jones 2013-03-04 17:51:31 -06:00
parent bef513721d
commit 559f2e2c58
2 changed files with 22 additions and 15 deletions

View File

@ -57,12 +57,14 @@
2006 2010 2014 2006 2010 2014
"Vancouver"]}] "Vancouver"]}]
["06_functions" {"__" [20 ["06_functions" {"__" [81
10 5 20
30 2 10
15 60
20 *] 15]
"___" [(fn [f] (f 5)) "___" [+
*
(fn [f] (f 5))
(fn [f] (f 5))]}] (fn [f] (f 5))]}]
["07_conditionals" {"__" [:a ["07_conditionals" {"__" [:a

View File

@ -4,26 +4,31 @@
(defn square [n] (* n n)) (defn square [n] (* n n))
(meditations (meditations
"Functions are often defined before they are used" "Calling a function is like giving it a hug with parentheses"
(= __ (square 9))
"Functions are usually defined before they are used"
(= __ (multiply-by-ten 2)) (= __ (multiply-by-ten 2))
"But they can also be defined inline" "But they can also be defined inline"
(= __ ((fn [n] (* __ n)) 2)) (= __ ((fn [n] (* 5 n)) 2))
"Or using even shorter syntax" "Or using an even shorter syntax"
(= __ (#(* 15 %) __)) (= __ (#(* 15 %) 4))
"Short anonymous functions may take multiple arguments" "Even anonymous functions may take multiple arguments"
(= __ (#(+ %1 %2 %3) 4 5 6)) (= __ (#(+ %1 %2 %3) 4 5 6))
"One function can beget another" "One function can beget another"
(= __ ((fn [] (= 9 (((fn [] ___)) 4 5))
((fn [a b] (__ a b))
4 5)))) "Functions can also take other functions as input"
(= 20 ((fn [f] (f 4 5))
___))
"Higher-order functions take function arguments" "Higher-order functions take function arguments"
(= 25 (___ (= 25 (___
(fn [n] (* n n)))) (fn [n] (* n n))))
"But they are often better written using the names of functions" "But they are often better written using the names of functions"
(= 25 (___ square))) (= 25 (___ square)))