Welcome! Please see the About page for a little more info on how this works.

–1 vote
ago in Macros by
edited ago by

Hello and thank you for creating such an expressive and interesting language!

I've learned Lisp/Scheme-like languages before (in addition to experimenting with dozens of other both widely known and less known languages over the years, from multiple paradigms: concatenative, logical, low-level imperative, OOP, etc), but lately I've been learning Clojure and enjoying it!

Anyway though, the other day I was reflecting upon the fact that in programming languages (and in natural language) the order in which words appear in an expression has a very substantial effect on readability sometimes and on what parts of what is being communicated feel like they are being emphasized and brought to one's attention.

Consider, for example, the difference in visual/subjective emphasis between the following two equally valid Clojure expressions:

(assert (not (zero? 1)))
(-> 1 zero? not assert)

The former is the more traditional/direct way of writing it (and perhaps looks more natural to most people), whereas the later has stronger unidirectional flow of action and less visual distractions once you get used to it. However, unfortunately for the -> case, notice that though the -> expression is more "elegant" in a sense, it makes the visibility of the assertion worse by putting the assert keyword at the end.

It is harder to scan for the presence of assert at a glance in the -> case because we start reading each line at the left side, and the column position of whatever is on the left on each line is more consistent/predictable and hence easier to scan for at a glance.

Some people may also find "zero not" to sound too "Yoda-like" (akin to concatenative programming languages like Forth and Factor, like post-fix notation) whereas something like "not zero" may sound more natural to many people.

I also thought about how comp (compose functions) could be used here to keep the more natural or more expected "not zero" ordering, but then a function call cost may be imposed by doing so if the optimizer doesn't remove the function wrapper implied by use of comp.

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 are actually very easy to write, as you can see.

This allows you to now do the following:

(<- assert not zero? 1)

As you can see, this enables you to reorder the "threading"/pipeline macro 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).

It is also very intuitive and easy to remember because of how the arrow operators visually guide the reader towards whichever direction the data is flowing (i.e. left to right for -> vs right to left for the proposed <- operator here).

This especially is well suited to assertions because what is most important about an assertion for someone trying to scan 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 traditional/direct nesting of the underlying expressions, 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.

Adding it also would naturally discourage unnecessary use of the computational overhead of comp when users prefer a right to left ordering of flow over a left to right one.

So, it seems clear to me that both orderings should be in every Clojure programmer's toolbox for legibility and I would argue that this should even be made part of the standard library since it is such a common case.

One can also of course just as easily add <-as too using the same strategy as above. Indeed, I already have added all these variants into my own code, though I more often use let sequences than <-as, since let is much more general in complex cases of course, for the obvious reasons of flexibility and clarity. So, <-as is maybe slightly fringe, but still good to add for symmetry purposes.

The reverse-direction <- and <<- threading macros though I am finding myself using quite often, and I likewise still use -> and ->> in data transformation code. Having both directions available feels analogous to having both active and passive voice (as in English) now available, whereas before with only one direction I was only writing in one voice whenever I used threading macros, which felt sometimes much more awkward and less clear (as the assertion example's legibility with -> vs <- clearly shows).

Interestingly, if you think about it, at least typically, -> corresponds with English passive voice because it begins with a noun (the initial data to be subsequently transformed), whereas <- corresponds with English active voice because it begins with a verb (the action ultimately being done to the whole). Well, at least, that analogy seems roughly true for "data as data" (not "data as code") cases anyway, in a sense.

Nice food for thought though, right? I really like these kinds of nuances. Truly great English writing takes very fluent advantage of such nuances and it makes sense that the same would apply to code if you gave a language enough mechanisms to actually do so fully.

As such, I am personally really happy with the resulting freedom of these macros!

With these new macros in hand one is much less likely to use the less legible deeply nested or oddly directed forms when you can now order things either way to fit each stylistic case.

Thanks for reading and for your time!

I hope to see this in the standard library one day and I think it'd make Clojure feel even more elegant, expressive, and conceptually symmetrical. I see only upside to that! Thinking flexibly and naturally after all is one of the most defining traits of Lisp/Scheme languages!

PS: These arrows operators look even nicer with a ligature font such as Fira Code, making them pop out even more clearly when reading through code. It results in a truly beautiful visual flow of code, seeing these arrows direct the eyes and the mind so intuitively.

ago by
**Here's an essentially salient point, perhaps among the strongest of arguments in favor of the idea:**

Clojure already has `comp` and hence *already has* heavy use of right to left ordering and semantics of exactly the same kind as the `<-` and `<<-` idea I've advocated for here, except that the `<-` variant here runs in compile-time and hence has no performance cost in run-time.

Therefore, any argument that the semantics of `<-` are somehow weird or would confuse people cannot possibly actually be true, given that `comp` already exists (and similar variants are used in other Lisp/Scheme languages) and it is widely used and clearly has not imposed any substantive confusion on the community.

By that reasoning (i.e. if `<-` actually were confusing or unworthy of inclusion), then you would expect to see people in the Clojure community up in arms about the presence of `comp` and claiming it is too much of a liability to be in the standard library and yet this is clearly not the case. The `<-` variant simply does the same thing but without as much of a potential performance penalty (and likewise for the other variants).

In light of this, by *reductio ad absurdum*, one cannot from an objective and rigorous standpoint find `<-` bad while still pretending that the existence of `comp` isn't. Hence my idea is therefore less merely an opinion and more an objective fact, since this argument essentially *proves* that it cannot be a liability if `comp` is not a liability, and it isn't.

That, to me, closes the case basically, regardless of what the community and/or stewards decide, though they'd clearly benefit from favoring it I think, as this logic demonstrates.

--------------------------------

Tangentially though, I want to also include an important personal remark:

I came to this community initially very excited by the combination of its expressiveness, practical access to libraries for creating real software with the JVM and its libraries, and especially the Clojure founder's (Rich Hickey's) reputation for being anti-AI (which is absolutely essential to finding a wholesome and ethically grounded and sane future for software and the tech industry).

I also earnestly hoped that this community would be the kind of place where I could easily share thoughts based firmly from first principles and have those thoughts evaluated fairly and with the kind of deep intelligence and objectivity for seeing with "fresh"/"newborn"/unbiased eyes that Lisp/Scheme languages known for regarding expressing concepts elegantly and without arbitrary preconceptions.

I was excitedly planning to invest deeply in using Clojure for real software and even had a great book idea planned to try to popularize it more by combining it with additional fresh insights I've built up over the years from experimenting with dozens of different languages in such a way as to help non-Lisp programmers see its merits when presented the right way so as to eliminate their fears and perhaps (hopefully) help finally break that barrier in large-scale adoption by doing so.

I had felt so very very enthused about it all that I felt all but certain that I'd write a book about it and make multiple major projects with it, but my interactions with the community have honestly almost completely destroyed most of that enthusiasm at this point. I have been disillusioned of my hopes for building a long term home here for my software and my writing and advocacy, frankly.

Several years ago I'd even written a textbook on deductive reasoning in mathematics and the foundations of logic that became #1 in its category briefly, so I already know for a fact that I have the capacity to write well enough to get real eyeballs on something and to bring clarity and truly new insights into things.

However, when I shared this (extremely trivial and blatantly obviously beneficial) idea on Reddit and now here and in *both* cases encountered a unwillingness to engage with things on first principles even in a case as extremely trivial as this one (i.e. for something literally already present in the library, but now just more performant and symmetrical in the case of this suggestion), and was also even harassed online (especially on Reddit) about it over the past couple days despite its objectively true nature, as even trivial reasoning demonstrates as above, it has really put me off.

This morning, I also logged into Reddit to attend to my threads, and was shocked to see an unexpected username that I thought to myself I surely must have only accidentally blocked on my Reddit account (`alexmiller` himself, the current steward of Clojure apparently as I've learned from more research today) and I went to correct the problem and unblock him. I then realized though, looking at the post history, why I'd blocked him: I block all users who post pro-AI threads and he recently (over the past day or two) posted one called "Why Lisp Thrives In the Age of AI"... Yet a huge part of why I was even willing to consider investing in building software on Clojure and in enthusiastically trying to build the community to be bigger was precisely because of Rich Hickey's reputed firmly anti-AI stances!

The fact that Alex Miller (the current language steward) is therefore apparently pro-AI in all likelihood now it seems, seems to be contrary to Rich Hickey's philosophies and personal integrity that played such a huge role in attracting me to build a future upon Clojure in the first place. I cannot truly trust any foundations that aren't built on ethical integrity and firmly uncompromising respect for human rights, etc.

Thus, despite Clojure's beauty an expressiveness, it is now seeming most likely that I will be abandoning the language and community on both moral grounds and intellectual grounds, since it was only the combination of both higher morals and the prospect of being able to share freshly imaginative ideas in a relatively objective social milieu that attracted me, but both have now evidence against those prospects and needless interpersonal pain and thereby almost all of my previous extremely high enthusiasm for adopting the language has been obliterated.

I need a place to build a real future on and I had really hoped it would be Clojure, but unless both aspects of why I have been put off change (i.e. unless control is no longer under any form of pro-AI influence and even thoughts as simple as ideas that are objectively already part of the language but just improved aren't dismissed based on essentially nothing) then this seems like the wrong place to build a truly lasting future given that my aspirations are to truly maximize language design and software quality and produce strictly ethically grounded software, but I apparently cannot after all do that here, much to my pain, contrary to what I had previously assumed and so very earnestly and passionately hoped for.

Regardless though, I thank you for all your time anyway and wish you all the best and still hope that things will turn around for Clojure, such as by returning to honoring Rich Hickey's moral principles again and being open to seeing code structure with truly logically rigorous "newborn"/objective eyes again, because otherwise the motivation to be here just isn't there anymore and I see no real future for the language in the absence of those essentials, especially for my very ethically strict and logically expansive and all-inclusive goals.

The good fight is always worth fighting, no matter the cost. That is the only real path to a good future for us. "AI" is inherently incompatible with that, seeing as it is effectively de facto digital enslavement of the rest of humanity by dystopian centralized powers.

Putting all that aside though: have a wonderful day/night/etc, to anyone reading this, whatever the case may be and however life turns out! I still hope for the best here, and everywhere, somehow and someway!

1 Answer

0 votes
ago by

Thanks for the suggestion, I don't think we're going to add these, but we can leave this post open to collect votes.

ago by
Ok, thank you for your time and for responding.

Leaving it open for votes (and also for anyone to stumble on the idea to possibly benefit from it) at least gives it a chance. I hope it goes well, since there is only upside to it.

One the other hand, almost none of the posts on this site appear to get a statistically significant number of votes (the highest voted item on the most recent entire 10 pages was merely +11 and most other items receive at most +4 and even that rarely). There's hence not likely to be a reliable signal either way from a statistical standpoint for votes. Thus, it would still seem to me that adding it on principle for symmetry would be best.

Hopefully additional Clojure devs will see this and read my argument and see the very clear benefits to allowing both directions of expression for enhanced readability (and also for slightly enhanced performance versus unnecessary use of `comp` for ordering).

Anyway, have a wonderful day and best of luck in your Clojure coding to all reading this!
...