unifying introductory functions a bit

This commit is contained in:
Colin Jones 2010-05-06 21:56:43 -05:00
parent 608690dca3
commit b5885e8867
3 changed files with 16 additions and 16 deletions

View File

@ -1,16 +1,10 @@
Tuples - syntax Tuples - syntax
Pattern Matching Pattern Matching
Functions returning functions
Functions taking functions
if expressions
for comprehensions
immutability/side effects immutability/side effects
state identity lifetime state identity lifetime
map, reduce, filter
lists / cons
memoization memoization
lazy sequences lazy sequences
recursion recursion
recrusive list processing recursive list processing
currying / pfa currying / pfa
reflection reflection

View File

@ -1,10 +1,16 @@
(meditations (meditations
"Your will face many decisions" "You will face many decisions"
(= __ (if (false? (= 4 5)) :a :b)) (= __ (if (false? (= 4 5)) :a :b))
"Some of them leave you no alternative" "Some of them leave you no alternative"
(= __ (if (> 4 3) [])) (= __ (if (> 4 3) []))
"And in such a case you may have nothing" "And in such a case you may have nothing"
(= nil (if (nil? __) [:a :b :c]))) (= nil (if (nil? __) [:a :b :c]))
"In others your alternative may be interesting"
(= :glory (if (not (empty? ())) :doom __))
"Or your fate may be sealed"
(= __ (if-not (zero? __) 'doom 'doom)))

View File

@ -1,19 +1,19 @@
(defn count-items [& items] (defn raise-to-the-tenth-power [n]
(count items)) (Math/pow n 10))
(meditations (meditations
"Functions are often defined before they are used" "Functions are often defined before they are used"
(= __ (count-items :a :b :c :d)) (= __ (raise-to-the-tenth-power 2))
"But they can also be defined inline" "But they can also be defined inline"
(= __ ((fn [n] (Math/pow n 10)) 2)) (= __ ((fn [n] (Math/pow n __)) 2))
"Or using even shorter syntax" "Or using even shorter syntax"
(= __ (#(Math/pow % 10))) (= __ (#(Math/pow % 10) __))
"Functions can beget others" "One function can beget another"
(= __ (((fn [] (= __ (((fn []
(fn [a b] (+ a b)))) (fn [a b] __ a b)))
4 5)) 4 5))
"Higher-order functions take function arguments" "Higher-order functions take function arguments"