One of my personal favorite new additions to Emacs 25 is, in fact, completely
invisible to most users. The new macros if-let and when-let, although simple
in purpose, are a delight to use and are frequently finding their way into my
code. The other two additions, thread-first and thread-last, are a bit more
specific, and take a bit getting-used-to if you’ve never seen them before.
The two *-let macros are easier to explain by just expanding them. All they do
is summarize a very common situation for lisp programmers. Instead of writing
this:
you can write this:
Seeing this example it shouldn’t be hard to understand when-let as well.
As you can see, it’s not exactly a revolution — you barely even save any
typing. But that extra level of nesting you save, along with an added bit of
clarity, for some reason makes me smile every time I get to use them.
The threading macros are something you see all the time in Clojure (under a
shorter name) but are probably not going to be as common in Emacs-Lisp — your
average Elisp code is far less functional than Clojure code. Still, they’re good
to know as they can be a huge help in some situations. Explaining again by
example:
If the examples above don’t look too useful to you, that’s because they’re not.
thread-first and thread-last are relatively short names by Elisp standards,
but they’re still long enough that you’re almost always going to be typing
more, instead of less, when you use these macros. Instead of saving space,
their value lies in improving readability of some rather extreme scenarios
(which are all too common in Elisp). For instance, which one of the forms below
would you find easier to read?
Finally, for those of you who use my speed-of-thought-lisp package, it already
has abbrevs for these macros under il, wl, tf, and tl.
New in Emacs 25.1: More flow control macros
22 Feb 2016, by Artur Malabarba.New in 25.1 post series
One of my personal favorite new additions to Emacs 25 is, in fact, completely invisible to most users. The new macros
if-let
andwhen-let
, although simple in purpose, are a delight to use and are frequently finding their way into my code. The other two additions,thread-first
andthread-last
, are a bit more specific, and take a bit getting-used-to if you’ve never seen them before.The two
*-let
macros are easier to explain by just expanding them. All they do is summarize a very common situation for lisp programmers. Instead of writing this:you can write this:
Seeing this example it shouldn’t be hard to understand
when-let
as well.As you can see, it’s not exactly a revolution — you barely even save any typing. But that extra level of nesting you save, along with an added bit of clarity, for some reason makes me smile every time I get to use them.
The threading macros are something you see all the time in Clojure (under a shorter name) but are probably not going to be as common in Emacs-Lisp — your average Elisp code is far less functional than Clojure code. Still, they’re good to know as they can be a huge help in some situations. Explaining again by example:
If the examples above don’t look too useful to you, that’s because they’re not.
thread-first
andthread-last
are relatively short names by Elisp standards, but they’re still long enough that you’re almost always going to be typing more, instead of less, when you use these macros. Instead of saving space, their value lies in improving readability of some rather extreme scenarios (which are all too common in Elisp). For instance, which one of the forms below would you find easier to read?Finally, for those of you who use my speed-of-thought-lisp package, it already has abbrevs for these macros under
il
,wl
,tf
, andtl
.Tags: emacs-25, emacs,
Commands to thread and unwind code in Emacs-Lisp »
« New in Emacs 25.1: map.el library
Related Posts
Emacs 25 is out! What are the new features and what were my predictions in emacs-25
New in Emacs 25.1: Archive priorities and downgrading packages in emacs-25
New in Emacs 25.1: Filtering by status and archive in emacs-25
Content © 2019, All rights reserved. Icons under CC3.0.