💾 Archived View for g.codelearn.me › 2023-05-28-emacs_thread_macros.gmi captured on 2023-09-28 at 15:32:01. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-05-30)
-=-=-=-=-=-=-
Surprisingly for me I just discovered that Emacs Lisp has equivalent of Clojure's `->` and `->>` macros.
And I'm not talking about `dash.el`. I'm talking about built-in `thread-first` and `thread-last`.
If you're not familiar with these beautiful macros, the main goal is to pipe a value through multiple functions and return the result.
`->` passes a value as first argument of next function, then takes the result and passes it as first argument to next function, etc. `->>` does the same except that it passes the result as next function's last argument
Instead of having this:
(+ (- (/ (+ 5 20) 25)) 40)
we can have this:
(-> 5 (+ 20) (/ 25) - (+ 40))
It's way easier to modify and which is even more important - to read.
There is `dash.el` library that provides us with similar macros (and many more) but emacs has built-in `thread-first` and `thread-last`.
(thread-first 5 (+ 20) (/ 25) - (+ 40))
will work for you if you're on Emacs 25 or newer.
I don't write much elisp but every time I do I feel like I miss those `->`/`->>`.
Not anymore!