Last active
December 31, 2015 05:59
-
-
Save ianjuma/7944817 to your computer and use it in GitHub Desktop.
clojure koans on functions and closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn multiply-by-ten [n] | |
(* 10 n)) | |
(defn square [n] (* n n)) | |
(meditations | |
"Functions are often defined before they are used" | |
(= __ (multiply-by-ten 2)) | |
"But they can also be defined inline" | |
(= __ ((fn [n] (* __ n)) 2)) | |
"Or using even shorter syntax" | |
(= __ (#(* 15 %) __)) | |
"Short anonymous functions may take multiple arguments" | |
(= __ (#(+ %1 %2 %3) 4 5 6)) | |
"One function can beget another" | |
(= __ ((fn [] | |
((fn [a b] (__ a b)) | |
4 5)))) | |
"Higher-order functions take function arguments" | |
(= 25 (___ | |
(fn [n] (* n n)))) | |
"But they are often better written using the names of functions" | |
(= 25 (___ square))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment