💾 Archived View for g.codelearn.me › 2023-05-28-emacs_thread_macros.gmi captured on 2023-07-22 at 16:13:57. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-05-30)

-=-=-=-=-=-=-

Emacs has built-in thread macros

Intro

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`.

What are these

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

Example

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.

Emacs lisp equivalent

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!