clojure-koans/src/koans/functions.clj

22 lines
466 B
Clojure
Raw Normal View History

2010-10-29 15:07:11 +00:00
(defn multiply-by-ten [n]
(* 10 n))
2010-02-09 04:57:08 +00:00
(meditations
"Functions are often defined before they are used"
2010-10-29 15:07:11 +00:00
(= __ (multiply-by-ten 2))
2010-02-09 04:57:08 +00:00
"But they can also be defined inline"
2010-10-29 15:07:11 +00:00
(= __ ((fn [n] (* __ n)) 2))
2010-02-09 04:57:08 +00:00
"Or using even shorter syntax"
2010-10-29 15:07:11 +00:00
(= __ (#(* 15 %) __))
2010-02-09 04:57:08 +00:00
2010-05-07 02:56:43 +00:00
"One function can beget another"
2010-05-27 01:54:29 +00:00
(= __ ((fn []
((fn [a b] (__ a b))
4 5))))
2010-02-09 04:57:08 +00:00
"Higher-order functions take function arguments"
2010-05-07 03:07:07 +00:00
(= 25 (___
(fn [n] (* n n)))))