From b5885e88671b9aec804c417455ca5f8f4080a802 Mon Sep 17 00:00:00 2001 From: Colin Jones Date: Thu, 6 May 2010 21:56:43 -0500 Subject: [PATCH] unifying introductory functions a bit --- ideaboard.txt | 8 +------- src/koans/about_conditionals.clj | 10 ++++++++-- src/koans/about_functions.clj | 14 +++++++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/ideaboard.txt b/ideaboard.txt index e421235..77a8892 100644 --- a/ideaboard.txt +++ b/ideaboard.txt @@ -1,16 +1,10 @@ Tuples - syntax Pattern Matching -Functions returning functions -Functions taking functions -if expressions -for comprehensions immutability/side effects state identity lifetime -map, reduce, filter -lists / cons memoization lazy sequences recursion -recrusive list processing +recursive list processing currying / pfa reflection diff --git a/src/koans/about_conditionals.clj b/src/koans/about_conditionals.clj index 7dbe6b6..bebf971 100644 --- a/src/koans/about_conditionals.clj +++ b/src/koans/about_conditionals.clj @@ -1,10 +1,16 @@ (meditations - "Your will face many decisions" + "You will face many decisions" (= __ (if (false? (= 4 5)) :a :b)) "Some of them leave you no alternative" (= __ (if (> 4 3) [])) "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))) diff --git a/src/koans/about_functions.clj b/src/koans/about_functions.clj index 0c5aac3..c0f33b5 100644 --- a/src/koans/about_functions.clj +++ b/src/koans/about_functions.clj @@ -1,19 +1,19 @@ -(defn count-items [& items] - (count items)) +(defn raise-to-the-tenth-power [n] + (Math/pow n 10)) (meditations "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" - (= __ ((fn [n] (Math/pow n 10)) 2)) + (= __ ((fn [n] (Math/pow n __)) 2)) "Or using even shorter syntax" - (= __ (#(Math/pow % 10))) + (= __ (#(Math/pow % 10) __)) - "Functions can beget others" + "One function can beget another" (= __ (((fn [] - (fn [a b] (+ a b)))) + (fn [a b] __ a b))) 4 5)) "Higher-order functions take function arguments"