2015-09-13 05:28:39 +00:00
|
|
|
(ns koans.13-creating-functions
|
2014-05-02 21:37:11 +00:00
|
|
|
(:require [koan-engine.core :refer :all]))
|
2014-01-26 00:04:22 +00:00
|
|
|
|
2010-11-17 04:23:33 +00:00
|
|
|
(defn square [x] (* x x))
|
|
|
|
|
|
|
|
(meditations
|
|
|
|
"One may know what they seek by knowing what they do not seek"
|
|
|
|
(= [__ __ __] (let [not-a-symbol? (complement symbol?)]
|
|
|
|
(map not-a-symbol? [:a 'b "c"])))
|
|
|
|
|
|
|
|
"Praise and 'complement' may help you separate the wheat from the chaff"
|
|
|
|
(= [:wheat "wheat" 'wheat]
|
2011-10-26 02:55:54 +00:00
|
|
|
(let [not-nil? ___]
|
|
|
|
(filter not-nil? [nil :wheat nil "wheat" nil 'wheat nil])))
|
2010-11-17 04:23:33 +00:00
|
|
|
|
|
|
|
"Partial functions allow procrastination"
|
|
|
|
(= 20 (let [multiply-by-5 (partial * 5)]
|
|
|
|
(___ __)))
|
|
|
|
|
|
|
|
"Don't forget: first things first"
|
|
|
|
(= [__ __ __ __]
|
2011-10-26 02:55:54 +00:00
|
|
|
(let [ab-adder (partial concat [:a :b])]
|
|
|
|
(ab-adder [__ __])))
|
2010-11-17 04:23:33 +00:00
|
|
|
|
2011-01-03 22:12:39 +00:00
|
|
|
"Functions can join forces as one 'composed' function"
|
2010-11-17 04:23:33 +00:00
|
|
|
(= 25 (let [inc-and-square (comp square inc)]
|
|
|
|
(inc-and-square __)))
|
|
|
|
|
2010-11-17 15:55:16 +00:00
|
|
|
"Have a go on a double dec-er"
|
2010-11-17 04:23:33 +00:00
|
|
|
(= __ (let [double-dec (comp dec dec)]
|
|
|
|
(double-dec 10)))
|
|
|
|
|
2011-01-03 22:12:39 +00:00
|
|
|
"Be careful about the order in which you mix your functions"
|
2010-11-17 04:23:33 +00:00
|
|
|
(= 99 (let [square-and-dec ___]
|
2011-10-26 02:55:54 +00:00
|
|
|
(square-and-dec 10))))
|