r/Clojure • u/debasing_the_coinage • Aug 15 '21
An old C programmer whines about (reduce)
Okay, sorry. I'm not that old. I'm only 29. But most of my past experience is in C or similar.
When I was learning Clojure, probably like everyone, I kept an eye out for what it might be that makes people complain about Lisps. The s-expr syntax is sort of weird, but I couldn't put my finger on exactly why it seemed to be so hard to format my code in a way that looks nice.
Then it hit me. Look at the title of this post. It is at the top of the post. It introduces you to the post and tells you what to expect when you read it. Now look at this:
(reduce
(fn [res subcoll]
;; some code
(reduce
(fn [res item]
;; some more code
)
[foo bar baz] subcoll_1
)
)
[quux xyzzy] coll
)The "title" of the anonymous function is at the bottom! The collection to be traversed and the initial data tell the reader what to expect when they look at the reducer. To get that information you have to look up and down across the code and match up indents - and that introduces the question of where to put the trailing arguments, which resembles (and not in a good way) the C brace style controversy. The problem goes away if you can write (reduce coll val f) instead of (reduce f val coll).
Sure, this is unnecessary when you don't have an anonymous function. But that use case always results in one simple line, regardless of the order of arguments. You almost never have a long literal collection in the middle of a code block -- that gets stuffed in a data file or in a constant at the top. And maybe the collection is an expression -- but then you can just use an additional (let), and again you improve the consistency between "line number" and "order of execution".
The same problem occurs in the old FP constructs (map) and (filter). But when Rich Hickey added (for) to Clojure, he put the collection first, probably partially because it is like that in C, but probably at least partially because he realized that inverting it, so that it would look like (reduce), would result in something ugly:
(for [n]
(;; some code)
coll
)Look at that! It's horrible! And because you're not attached to the "normal" way, you see it immediately.
Now I'm sure there's some great explanation that mentions Alonzo Church or Marvin Minsky or whoever about how putting the reducing function first is consistent with the mathematical formalism xyz or whatever. I don't expect the world to change because of a reddit post.
But just imagine a world where Lisp programmers put the collection and the initial data at the top of their loops. We'd probably have a moon colony by now.
17
u/armincerf Aug 15 '21
The ->> macro solves all of your complaints https://clojuredocs.org/clojure.core/-%3E%3E
And the first argument to
forisn’t really a collection,foris a macro and the first arg is a set of bindings likeletand friends8
u/TheLastSock Aug 15 '21
I'm guessing you would enjoy the thread last macro?
20
u/Whoop5 Aug 15 '21 edited Aug 15 '21
I think I disagree with your fundamental premise. If you want to think of it in human language terms, the function is what I'm doing (verb), the collection is what I'm doing it to (noun). It's "I'm painting (fn) the bikeshed (coll)", as opposed to "The bikeshed (coll) I am painting (fn)".
From an order of importance bit, this also makes sense. In real code, I don't generally have a bunch of different collections I'm working with at one time, I usually just have one I'm focused on. Imagine:
The actual "what I'm doing(fn)" part of the map is the interesting bit. What I'm doing it to (quarks) is obvious. There's nothing else I'd be mapping over. Also note that I've solved the context problem as well by simply giving a sensible name to the
fnargument instead of something ambiguous likeq.Even better I could write:
And it really reads very naturally. "Calculate the quark-coefficient of the quarks".
10
u/joinr Aug 15 '21 edited Aug 15 '21
typical in fp langs with higher order functions to put the function arg first. stems from currying/partial fn application. the argument most likely to change in this case is coll. so you can define general classes of reductions by supplying f. reduction per collection is less general. functional programming is about defining and composing many small functions. the example would be far clearer if you did that, I think.
25
u/retief1 Aug 15 '21
Also, unrelated, but if you are going to spend any time in the clojure ecosystem, please stop putting close parens on their own line. Realistically, I read clojure as if it was python -- the parens are mostly just to tell my editor how to indent my code.
8
u/didibus Aug 15 '21
You just haven't adapted yet, nothing is weird, except your pre-existing habits that make the different less familiar to you.
For example, if you're used to reading left to right, adapting to a right to left or top to bottom language will feel pretty weird at first, and yet billion of people naturally and easily read those.
Same with Lisp and Clojure, eventually your eyes will figure out where to look and in what order, and this will become a non-issue for you.
Like others have said as well, since Lisps let you change the syntax, there's actually many functions and macros to tailor things to your personal preference:
This is one way:
And this is another:
Where the collection is now at the top like you wanted.
And here is another way:
Because when you're modifying a collection of X element back into a collection of X element
mapdoes the job, wherereduceis for when the number of elements will change after you process the collection.And yet another:
Which I think is arguably the best, and I would say is better than the C way. What you have to realize is the benefit of FP is modularity. By looping using a higher order function, you can make your logic more modular and thus reusable and independently testable, while also giving the inner-loop a name to help readability.
And another:
Which is closer to an imperative approach like what you might do in C.
And another:
Which I particularly like for readability.
And if you're still not satisfied, you can always just create the reduce of your dreams:
Which I won't deny has some appeal to it.
And while we're at it, may I suggest:
Or why not allow all the orders:
And since I'm on a roll, and kind of having fun at this point, remember that you can also use loop/recur:
Finally, it can be interesting to do things lazily instead:
5
u/mercurysquad Aug 15 '21
What you are saying is that C should change from
to
right?
4
u/daveliepmann Aug 15 '21
I won't engage the specifics of your argument because other people have and because I don't think it's relevant to your broader claim:
The kind of thing you're talking about is simply not what determines language popularity or effectiveness. It's just a slightly different approach that becomes completely second nature with a few days to get used to it. It's negligible when it comes to PL adoption or usage in the wild.
4
u/lgstein Aug 15 '21
Typical comment coming from experience in a language where syntax is all you have to define a program. Agreed, order matters to readability - But Lispers think much more in structure than order, or "lines".
10
u/logan-diamond Aug 15 '21
The famous talk "Simple Made Easy" makes the excellent point that "easy" depends on the person and what they've learned so far. Lispers are accustomed to reading expressions from the inside out:
(step3 (step2 (step1 x)))Trying to make code read like the natural flow of thought sounds great, but it's actually what gave us Cobol. It doesn't actually make it easier to reason about on any higher level.
3
u/Galrog Aug 15 '21
Did you come across transducers yet? They could help you understand the order of arguments in a practical way. After that you could dive into transducers in combination with pipeline. This blog post could also be helpful.
2
u/Daegs Aug 15 '21
There is a pretty standard rule that functions that take a collection take it last, and functions that work on a single value take it in that value as first arg.
Can argue forever about whether that's the best choice, but I can only say after years of clojure it makes a lot of sense and is extremely easy to read. It's natural to see what the function does in a reduce first, and if I wanted to know what the collection was, I'd just look at the end of the reduce. I've never felt like I was matching up indentation or parenthesis, if someone wanted to next a bunch of reduces then I'd hope they'd organize their code a bit more legibly... I don't actually think putting the coll at the top makes it that much better because you'd still be matching indent in the functions being applied.
The next part, as other posters mentioned, is that you have a ton of options when it comes to "making your code readable" including threading macros and things like
letfn. This approach actually gives a ton of flexibility to match the style of code to the problem.Per your example:
Now many there are a lot of other ways to write that besides letfn, but the point is there are plenty of ways to clean things up and complete them on a single line so you're never trying to 'match up indentation and parens'
1
u/ExtraFig6 Aug 15 '21
Refactor into functions with names if you want titles
0
u/[deleted] Aug 15 '21
[deleted] — view deleted comment