Fork me on GitHub
#braveandtrue
<
2018-12-19
>
felipebarros06:12:07

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.

felipebarros06:12:21

The return value being a function, the arguments being functions, the order of the arguments is inverse to first and rest, etc.

jaide07:12:05

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

felipebarros01:12:18

I'm curious to see your solution 🙂 what is a pipe function? And its the first time I see butlast, thanks!

felipebarros01:12:32

I mean, I have used | and others in the shell, but in Clojure I'm not sure what you mean by it.

jaide02:12:58

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.

💯 4
felipebarros04:12:55

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 🙂

felipebarros05:12:55

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. 😄

👍 4
manutter5111:12:34

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 😛

😅 4
manutter5111:12:20

@anantpaatra I can help you with figuring out that bit if you want.

felipebarros01:12:07

I do need help with this, because I can't seem to find a way out of it. Even with your reduce tip.

felipebarros01:12:02

The source for comp uses something called reduce1 when the arity of the function is 3 or more, and it also uses list*, which I believe is to "flatten" a list (but I've never used or studied flattening, only read people mentioning about it). You mean using reduce like that?

manutter5112:12:50

@jayzawrotny There is a way to implement my-comp using recursion, that will naturally give you the last-to-first order without reverse. 😉

jaide15:12:15

Ah yes, you’re right!

felipebarros01:12:02

The source for comp uses something called reduce1 when the arity of the function is 3 or more, and it also uses list*, which I believe is to "flatten" a list (but I've never used or studied flattening, only read people mentioning about it). You mean using reduce like that?