I am currently reading the book "The Joy of Clojure", 2nd edition. In Chapter 7 "Functional Programming", I came across the following function that was used to describe function composition.
(defn fnth [n]
(apply comp
(cons first
(take (dec n) (repeat rest)))))
((fnth 5) '[a b c d e])
;=> e
I don't fully understand the mechanics of that function.
Trying to reproduce the inner scope without using comp, the function produces a repeating list:
(take (dec 5) (repeat (rest '[a b c d e])))
;=> ((b c d e) (b c d e) (b c d e) (b c d e))
What is the point of attaching first to that repeating list by using comp?
Is this also the last part of the function list which is fed into comp?
(first (take (dec 5) (repeat (rest '[a b c d e]))))
=> (b c d e)
Thank you in advance!