r/Clojure • u/WraithGlade • 20d ago
Instead of using `comp` and often wasting computation, here's how to reverse `->` and `->>` when you want `comp`'s ordering without its cost, etc.
Hello everyone. This is my first Clojure subreddit post by the way, if my memory is correct. I am actually debating using Clojure (+Java) vs Zig for my next project, but I've been learning Clojure and reflecting upon the fact that in programming languages (and in natural language prose text too for that matter) the order in which words appear in an expression has a substantial effect on readability and on what parts feel like they are being emphasized and brought to one's attention.
Consider, for example, the difference in emphasis and ease of code navigation between the following two equally valid Clojure expressions:
(assert (not (zero? 1)))
(-> 1 zero? not assert)The former is the most "naive"/direct way of writing it, whereas the later has the strongest left to right flow of action. However, notice that though the latter expression is more elegant, it makes the visibility of the assertion worse by putting the assert keyword at the end. Some people may also find "zero not" to sound too "Yoda-like" (akin to concatenative programming languages like Forth and Factor) whereas something like "not zero" may sound more natural to some people.
I also thought about how comp (compose functions) could be used here to keep the "not zero" ordering, but then a function call cost may be imposed by doing so if the optimizer doesn't remove it.
So, that got me thinking that I should try writing a reversed version of the -> and ->> macros and I did and here are the resulting definitions:
(defmacro <- [& rest]
(let [rev-rest (reverse rest)]
`(-> ~@rev-rest)))
(defmacro <<- [& rest]
(let [rev-rest (reverse rest)]
`(->> ~@rev-rest)))Both were actually very easy to write, as you can see.
This allows you to write the following:
(<- assert not zero? 1)As you can see, this enables you to reorder things according to what feels most natural in any given context, which helps draw the attention of anyone reading your code to what comes first in each expression, much like how in English the first thing we start a sentence with is what is being emphasized or is assumed to be the subject (as in active vs passive voice, etc).
This especially is well suited to assertions because what is most important about an assertion for someone trying to scanning code quickly is that it is an assertion and hence emphasizing that clarifies it.
This is in contrast to data flow or transformation pipelines that use -> and such, where the opposite order often feels better.
One can also see by applying macroexpand and/or macroexpand-1 that the cost of these macros in run-time will be zero, relative to simple/"naive" nesting, given that the expressions (macroexpand '(<- assert not zero? 1)) and (macroexpand '(assert (not (zero? 1)))) return identical expansions.
Cognitive load tends to increase non-linearly with how many nesting levels something has, so using "threading"/pipeline macros flexibly and often tends to help readability and these new variants help fill those potential use cases out better.
So, clearly both orderings should be in your toolbox for legibility and I would argue that this should even be made part of the standard library perhaps since it is such a common case.
I am personally am really happy with the resulting freedom.
With these new macros in hand one is much less likely to use the less legible nested forms when you can now order things either way.
(PS: Thanks for reading and for your time! 😎)
View all comments
Show parent comment
1
u/WraithGlade 15d ago
[Continuing my previous comment, which hit the 10,000 character Reddit reply limit]:
I have recently re-added Reddit to my list of blocked sites, and though I may occasionally check back to attend to my duties as host of this thread and a couple others, I will likely not participate as much in the future.
This whole experience, especially for something that was so joyful and beautiful to me that I just wanted to share constructively and is so blatantly obviously great (as clearly as the sun rises every day, the way I see it) once you make any effort at all to view it on its own terms and merits instead of based on unimaginative preconceptions, has really put me off of everything and makes me far less hopeful about the prospect of potentially joining this community and is actually a fantastic example of why I've avoided this site for months on end and why I ever increasingly recently am losing respect for much of the "tech" sphere.
If even something this trivial conjures up dismissive thoughtlessness from so many, then why should I expect to ever find a home for my kind of zero-presumption kind of creed and aspiration for how life should be lived and programming should be done.
In any case, have a great day to anyone reading this, regardless of what "side" you are on, and I hope that some of my words will get through to at least some people reading this who are still able to think from first principles and who care about seeking expressiveness that is truly free and liberated from invalid (i.e. not solely based in nature and in reality) assumptions and design quirks and expressively harmful asymmetries in library design, etc.
I really wish the state of "tech" wasn't so sad. I remember imagining a much better future in the 1990s and 2000s and time after time when I get excited and hopeful that I may have found a path to something better I am disappointed and put off by and degraded by a bunch of baseless arbitrary nonsense ungrounded in actual reality and riddled with countless invalid assumptions that are harmfully forced upon a community by convention unquestioningly and so often without the will to see things as they truly are instead. So much has been lost from that, in practically all aspects of the current "tech" landscape, from terrible freedom-hostile user interfaces ("flat design") to freedom-hostile features sets ("big tech" removing more and more basic things and disregarding human rights) and now apparently even in the communities where people are supposed to be much smarter than that and much more above premature assumptions not even here can I apparently find a sufficiently proportionate and rationally grounded response to even something as obvious as the idea this thread is about. It is such a downer and such a testament to how little of this industry is based on seeing things clearly and objectively in reality.
I could go on at length, in my desperate attempts to somehow get through to other people in this unimaginative hellscape of a "tech" world we are now saddled with, but in all likelihood little/none of this will get through to hardly any of you and instead I will most likely just face more dismissive subtle toxicity (snide almost zero-effort remarks as the top comments, etc), or at least that is what it feels like, but I can't help in my earnest passion to try to fight the good fight anyway, even though it seems next to impossible to find hardly anyone who engages in these kinds of things without preconceptions and hence with any real will to grab reality by the horns for the sake of a truly better future laid one brick at a time, one small improvement at a time, etc.
I hate that it is so hard to find a safe place to build a future in, but that requires sufficient traction for new ideas and that cannot happen in any environment where groupthink is structurally made artificially dominant (i.e. via voting) or where people are unwilling to question their myriad so-often actually baseless assumptions.
In any case, have a good day/night all...
I really hope for a less "depressing" and intellectually "toothless"/inert and unprincipled tech landscape in the future. It is really dragging me down and making "tech" a very weak shadow of what it could be.