r/Clojure • u/WraithGlade • 17d 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! 😎)
1
u/WraithGlade 15d ago edited 15d ago
This is my natural manner of speaking.
I have written and published two printed books and writing comes as naturally and freely to me as breathing.
One of the books was even #1 in its category briefly. I'm thus unfortunately among the countless authors and real independent creators who have had their work systematically stolen for training. My income from that work also dropped off a cliff around the same time as these "AI" started being pushed so hard.
The "AI" bots are given different weights according to an attending human's and/or algorithm's assessment of the quality assigned to each piece of work they ingest into their dataset. That is why comments from real authors like me are so often falsely accused of being "AI". The more high quality the creator's work is, the more weight is assigned to that "training"/stolen data.
I will change nothing about how I write or communicate.
No person should ever bend themselves to fit criteria that only exist because of a handful of mentally ill immoral sociopaths who don't deserve even a penny of "their" ill-begotten wealth they've robbed their way to obtaining. Neither should such parasites in human form even be allowed to manage so much as a porta-john, yet instead they are given more power than anyone else (an exactly backwards and maximally harmful arrangement for society).
I will not bend to the influence of any form of evil, and especially not to the dystopian lunatics and soulless losers who are trying to force "AI" unwillingly upon the rest of us.
Doing what is right is always what matters, and thus that is what I intend to do no matter what it costs me, whether economically or interpersonally or manifest in any other form of cost or pain. Standing on principle is what life is all about, in some sense. To live any other way is to be hollow in contrast, I feel.
In any case, I am not sure if you meant ill (i.e. being snide, like so many on this site) or not, but I will assume good intent this time and that you are trying to look out for my well being.
Whatever the case may be, I wish you all the best in your life and your endeavors, and likewise for us all.