New in Emacs 25.1: More flow control macros22 Feb 2016, by Artur Malabarba.
New in 25.1 post series
- New in Emacs 25.1: Query-replace history is enhanced.
- New in Emacs 25.1: Better Rectangles
- New on Elpa and in Emacs 25.1: let-alist
- New in Emacs 25.1: Easily install multifile package from a directory
- New in Emacs 25.1: comment-line
- New on Elpa and in Emacs 25.1: seq.el
- New in Emacs 25.1: Have prettify-symbols-mode reveal the symbol at point
- New in Emacs 25.1: Round quotes in Help buffers
- New in Emacs 25.1: Easily search for non-ASCII characters
- New in Emacs 25.1: EWW improvements
- New in Emacs 25.1: map.el library
- New in Emacs 25.1: More flow control macros
One of my personal favorite new additions to Emacs 25 is, in fact, completely
invisible to most users. The new macros
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-last, are a bit more
specific, and take a bit getting-used-to if you’ve never seen them before.
*-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
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-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