let-alist is the best thing to happen to associative lists since the
invention of the cons cell. This little macro lets you easily access the
contents of an alist, concisely and efficiently, without having to specify them
preemptively. It comes built-in with 25.1, and is also available on GNU Elpa for
older Emacsen.
If you've ever had to process the output of a web API, you've certainly had to
deal with endless alists returned by json-read. I'll spare you the rant and go
straight to the example.
Here's a very simplified version of a function of the SX package, before let-alist.
And this is what the same function looks like now (again, simplified).
How much nicer is that? let-alist detects all those symbols that start with a
., and wraps the body in a let form essentially identical to the one above.
The resulting code is much nicer to write, and the byte-compiled result is
exactly as efficient as the manually written version. (If it's not
byte-compiled, there will be a performance difference, though it should be
small.)
And just to make things nicer, you can use this snippet to highlight those .
symbols as if they were keywords.
Update <2014-12-20 Sat>
Due to popular demand, let-alist now does nested alists. The example above
shows how you can use .owner.display_name to access the value of
display_name inside the value of owner.
New on Elpa and in Emacs 25.1: let-alist
15 Dec 2014, by Artur Malabarba.New in 25.1 post series
let-alist
is the best thing to happen to associative lists since the invention of the cons cell. This little macro lets you easily access the contents of an alist, concisely and efficiently, without having to specify them preemptively. It comes built-in with 25.1, and is also available on GNU Elpa for older Emacsen.If you've ever had to process the output of a web API, you've certainly had to deal with endless alists returned by
json-read
. I'll spare you the rant and go straight to the example.Here's a very simplified version of a function of the SX package, before
let-alist
.And this is what the same function looks like now (again, simplified).
How much nicer is that?
let-alist
detects all those symbols that start with a.
, and wraps the body in alet
form essentially identical to the one above. The resulting code is much nicer to write, and the byte-compiled result is exactly as efficient as the manually written version. (If it's not byte-compiled, there will be a performance difference, though it should be small.)And just to make things nicer, you can use this snippet to highlight those
.
symbols as if they were keywords.Update <2014-12-20 Sat>
Due to popular demand,
let-alist
now does nested alists. The example above shows how you can use.owner.display_name
to access the value ofdisplay_name
inside the value ofowner
.Tags: package, emacs,
Where do YOU bind expand-region? »
« Introducing Names: practical namespaces for Emacs-Lisp
Related Posts
It’s Magit! And you’re the magician! in package
validate.el: Schema validation for Emacs-Lisp in package
Beacon — Never lose your cursor again in package
Content © 2019, All rights reserved. Icons under CC3.0.