Clojure Koans 13 Creating Functions Notebook
Welcome! As with my previous notebooks in this series, I will:
answer each exercise in the Clojure Koans
attempt to distill out "micro-skills" relevant to each exercise
share any questions I have on the exercise
share other notes I think are useful
As always, be mindful that there are spoilers to the Clojure Koans below.
Link to the original Clojure Koans repository: https://github.com/functional-koans/clojure-koans/
{: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"}}}
{:hello (clojure-version)}
Initial Thoughts
I am having the sense that there may be some duplicates to the skills. Especially relating to functions, definitions, and invocations. As a reminder to myself:
[ O ] Create a single listing of all skills to easily locate & remove duplicates
(defn square [x] (* x x))
;; "One may know what they seek by knowing what they do not seek"
(= [true false true] (let [not-a-symbol? (complement symbol?)]
(map not-a-symbol? [:a b "c"])))
;; skill 13001: Flip a boolean function result by using the `complement` function
;; skill 13002: Define a local binding (?) by using the `let` function
;; skill 13003: Map over a collection with a custom named function defined and bound locally
;; "Praise and 'complement' may help you separate the wheat from the chaff"
(= [:wheat "wheat" wheat]
(let [not-nil? (complement nil?)]
(filter not-nil? [nil :wheat nil "wheat" nil wheat nil])))
;; description: TBD...
;; "Partial functions allow procrastination"
(= 20 (let [multiply-by-5 (partial * 5)]
(multiply-by-5 4)))
;; question 13001: What's the difference between `#(* % 4)` and `(partial * 5)`
;; question 13002: What is inferred/meant by "procrastination"?
;; skill 13004: TBD...
;; "Don't forget: first things first"
(= [:a :b :c :d]
(let [ab-adder (partial concat [:a :b])]
(ab-adder [:c :d])))
;; description: TBD...
;; "Functions can join forces as one 'composed' function"
(= 25 (let [inc-and-square (comp square inc)]
(inc-and-square 4)))
;; skill 13005: Combine multiple functions to run sequentially by using the `comp` function
;; "Have a go on a double dec-er"
(= 8 (let [double-dec (comp dec dec)]
(double-dec 10)))
;; question 13003: Do you see a viable skill or knowledge takeaway from this Koan (that isn't present in the preceding or following Koan exercises)?
;; "Be careful about the order in which you mix your functions"
(= 99 (let [square-and-dec (comp dec square)]
(square-and-dec 10)))
;; exercise 13001: Combine the square function and the decrement function by using the `comp` function (macro?) where a number is first squared and then second decremeneted
Closing Thoughts
I think the quality of my notebooks has dropped somewhat. It's important to acknowledge that not all days will be more productive than your average - that's just statistics - and then still, that doesn't mean I have to permanently accept low quality outcomes.
As I did yesterday with Notebook #8 (on conditionals), I will be going through the notebooks published so far (roughly one per day) to answer my own questions, and further refine my skill break-downs in terms of clearness, atomicity, and completeness.
I look forward to incorporating your feedback. If you have read this far, thank you, and I appreciate your efforts and readership.