A callable plist data structure for Emacs
Posted April 16, 2017 at 04:44 PM | categories: elisp, macro, emacs | tags:
Updated February 04, 2018 at 05:06 PM
Table of Contents
Emacs lisp has a few data structures that store key-value pairs. Here are some canonical examples of these data structures and the way to get data out of them.
- a-lists
(let ((data '((key1 . 4) (key2 . "tree")))) (cdr (assoc 'key2 data)))
tree
- p-lists
(let ((data '(:key1 4 :key2 "tree"))) (plist-get data :key2))
tree
- A hash table
(let ((data #s(hash-table data (key1 4 key2 "tree")))) (gethash 'key2 data))
tree
Each of these uses some function to get data out of them. I have been learning about closures today, and realized a way you can make a "callable" data structure using them. In a closure, the data is stored as part of a function. We will use a "let over lambda" with a defalias in a lexical environment to achieve this. I will wrap a p-list with this approach, but it could work with any of the examples above. We will make the function have a few behaviors that allow us to see the whole data structure with no args, to get a value with one arg that is a key, and to set a value if there are more than two args add them as key-val pairs to the data structure. This block binds the function to the symbol "d" which is then a callable function.
(let ((data '(:key1 4 :key2 "tree"))) (defalias 'd (lambda (&rest key-vals) (cond ;; no args, return data ((= 0 (length key-vals)) data) ;; just a key, get val ((= 1 (length key-vals)) (plist-get data (car key-vals))) (t (loop for key in (-slice key-vals 0 nil 2) for val in (-slice key-vals 1 nil 2) do (plist-put data key val)) data)))))
d
Now we can use it like to get some data out:
(d :key2)
tree
And add new values like:
(d :key3 "oak")
:key1 | 4 | :key2 | tree | :key3 | oak |
You can update a value with this too:
(d :key3 "pine")
:key1 | 4 | :key2 | tree | :key3 | pine |
or add multiple values like this:
(d :key4 0 :key5 9)
:key1 | 4 | :key2 | tree | :key3 | pine | :key4 | 0 | :key5 | 9 |
And see the whole plist with no args:
(d)
:key1 | 4 | :key2 | tree | :key3 | pine | :key4 | 0 | :key5 | 9 |
Pretty nice! It seems like there ought to be a macro to facilitate creating those. Here is one. This macro basically expands to the same code as above, but for fun I add a default value option.
(defmacro default-dict (var &optional default &rest key-vals) "Bind a callable plist to VAR that contains KEY-VALS." (let () `(let ((data ',key-vals)) (defalias ',var (lambda (&rest key-vals) (message "%s" key-vals) (cond ;; no args, return data ((= 0 (length key-vals)) data) ;; just a key, get val ((= 1 (length key-vals)) (or (plist-get data (car key-vals)) ,default)) (t (loop for key in (-slice key-vals 0 nil 2) for val in (-slice key-vals 1 nil 2) do (plist-put data key val)) data)))))))
Here is an instance of it.
(default-dict d2 "None" :key1 4 :key2 "tree")
d2
And here it is in use.
(d2 :key1)
4
(d2 :new-key)
None
Not bad. If you come from Python, you might find this style of data structure to be more similar to what you are used to seeing. It sure seems less verbose than the usual plist boilerplate I have used before.
1 An update
One (perhaps undesirable even) feature of the approach above is that it creates a function in the global namespace. This might have unintended consequences with name clashes or shadowing, and if you later use the same variable name for a plist, you would change the function behavior. Here we consider a way to limit the scope of where these functions exist and work. The labels macro provides one way to do this, we just create temporary functions that only exist within a scope. There is a lot of backticking and comma operators in this, and it took quite a few iterations to get it working!
This macro creates temporary functions for each keyword that return the value in the plist.
(defmacro with-dict (key-vals &rest body) "A context-manager for a plist where each key is a callable function that returns the value." (declare (indent 1)) (let* ((g (if (symbolp key-vals) (symbol-value key-vals) key-vals)) (keys (-slice g 0 nil 2))) `(labels ,(loop for key in keys collect (list key '() `(plist-get ',g ,key))) ,@body)))
with-dict
Here is how we use it:
(with-dict (:a 1 :b 'some-symbol :c 3) (:b))
quote | some-symbol |
We can also use it with variables that hold mappings like this.
(let ((d '(:key1 1 :key2 some-other-symbol :key3 3))) (with-dict d (format "We got %s" (:key2))))
We got some-other-symbol
That is pretty interesting! In case that looks similar to a context manager in Python, now you know where Python got that idea ;)
Another related idea is to let-bind the values to variables within a scope. We can't use the keywords directly here, so I use some hackery to strip off the colon so it is a regular symbol. That is not quite as nice I guess since you have to remember to remove the : from the symbols in the body of your code.
(defmacro with-plist-vals (plist &rest body) "Bind the values of a plist to variables with the name of the keys." (declare (indent 1)) `(let ,(loop for key in (-slice plist 0 nil 2) for val in (-slice plist 1 nil 2) collect (list (intern (substring (symbol-name key) 1)) val)) ,@body))
with-plist-vals
Here is an example usage.
(with-plist-vals (:a 4 :b 6) (* 2 a))
8
Obviously that is just an alternate syntax for the let statement, but it lets you leverage the plist syntax for multiple purposes.
Copyright (C) 2018 by John Kitchin. See the License for information about copying.
Org-mode version = 9.1.6