Avi Drucker / Apr 16 2021 / Published
Remix of Clojure by Nextjournal
Clojure Koans 9 Higher Order Functions Notebook
Spoilers alert for those who haven't yet tried to solve the Clojure Koans.
Let's get into it!
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
deps.edn
Extensible Data Notation
{:hello (clojure-version)}
0.1s
Clojure
Initial Thoughts
Similar to earlier sections, I strongly believe there is much more complexity under the surface to explore here.
;; "The map function relates a sequence to another"
(= [4 8 12] (map (fn [x] (* 4 x)) [1 2 3]))
;; description: An anonymous function (which multiplies something by four) is mapped over the vector "one two three." This mapping results in a new vector of "four eight twelve".
;; skill 9001: Map a function over a sequence to transform/change/mutate the sequence
0.1s
Clojure
;; "You may create that mapping"
(= [1 4 9 16 25] (map (fn [x] (* x x)) [1 2 3 4 5]))
;; skill 9002: Write an inline function to be passed to a mapping higher-order function
Clojure
;; "Or use the names of existing functions"
(= [false false true false false] (map nil? [:a :b nil :c :d]))
;; skill 9003: Pass an existing function to a mapping function
Clojure
;; "A filter can be strong"
(= () (filter (fn [x] false) (:anything :goes :here)))
;; question 9001: What is a "strong" filter?
Clojure
;; "Or very weak"
(= (:anything :goes :here) (filter (fn [x] true) (:anything :goes :here)))
;; question 9002: What is a "weak" filter?
Clojure
;; "Or somewhere in between"
(= [10 20 30] (filter (fn [x] (< x 40)) [10 20 30 40 50 60 70 80]))
;; skill 9004: Filter a vector/collection with a boolean condition
Clojure
;; "Maps and filters may be combined"
(= [10 20 30] (map (fn [x] (* x 10)) (filter (fn [x] (< x 4)) [1 2 3 4 5 6 7 8])))
;; skill 9005: Filter over a vector/collection and then map over it with a transformation function
Clojure
;; "Reducing can increase the result"
(= 24 (reduce (fn [a b] (* a b)) [1 2 3 4]))
;; skill 9006: Reduce a vector/collection of numbers by multiplying them together
Clojure
;; "You can start somewhere else"
(= 2400 (reduce (fn [a b] (* a b)) 100 [1 2 3 4]))
;; description: An "a times b" function is used to reduce over the vector "one two three four" where the initial accumulator value is 100. This reduction results in a result of 2400 (* 100 1 2 3 4).
;; skill 9007: Reduce with a programmer-supplied initial value/accumulator
Clojure
;; "Numbers are not the only things one can reduce"
(= "longest" (reduce (fn [a b]
(if (< (count a) (count b)) b a))
["which" "word" "is" "longest"]))
;; exercise 9001: Determine the longest word in a vector by supplying a custom compare function to reduce
Clojure
Closing Thoughts
Thanks for checking out my Clojure Koan notebook break-down!
For future updates to this notebook, I'd like to add content to clear up common confusions surrounding higher order functions (HOFs), illustrate effective ways to practice using HOFs, and break down HOFs into their atomic components.