Is it just me or asking the following at the fifth chapter is a bit too much? I definitely failed at that, and looking at the source for Clojure's comp
, I believe (or want to believe) that I may not be alone there.
> [..] try reimplementing Clojureโs comp
function so you can compose any number of functions.
๐
The return value being a function, the arguments being functions, the order of the arguments is inverse to first
and rest
, etc.
I just looked up my solution and realized I implemented it more like a pipe function where it goes from first to last. ๐ I know itโs not covered by the book but you could reverse it with http://clojuredocs.org/clojure.core/butlast and http://clojuredocs.org/clojure.core/last
I'm curious to see your solution ๐ what is a pipe function? And its the first time I see butlast
, thanks!
I mean, I have used |
and others in the shell, but in Clojure I'm not sure what you mean by it.
In functional programming a common alternative to comp is pipe where functions are applied top to bottom. Itโs very much the same principle with the Unix pipe you mentioned. Hypothetically in Clojure it would be like (pipe :a :b) instead of (comp :b :a). However Clojure has thread macros which are way better.
https://github.com/jayzawrotny/clojure-noob/blob/679955cbb11b689bba1f869ef500c5a3c8d8ec81/src/clojure_noob/chapter_5.clj#L188 itโs incorrect but not terribly far off
Wow, how come I haven't thought of just evaluating one function at a time and building a result? Instead I just assumed I had to return a function that was composed of other functions like loop
itself haha nice one ๐
And I never thought of thread macros as something similar to pipes... since English isn't my main language, I take a lot of time to associate different names to the same concept I guess. But yes, I've learned about Clojure's thread macros a while back and always end up using them. ๐
Chapter 5 is definitely one of the steeper parts of the learning curve. Chapter 4 is about using functions to process data, which is fine, thatโs what youโd expect functions to do. But in Chapter 5 he starts talking about using functions to process functions, and thatโs just weird ๐
@anantpaatra I can help you with figuring out that bit if you want.
@jayzawrotny There is a way to implement my-comp
using recursion, that will naturally give you the last-to-first order without reverse. ๐
Ah yes, youโre right!