|Project author||Jean-Philippe Paradis|
|Programming language||Common Lisp|
|Library type||Operator overlay macro|
|Date||22 oct 2012|
(Nov 2012 dist and later)
place-modifiers essentially gives access to hundreds of modify-macros through one single macro:
To use place-modifiers, simply
(:import-from #:place-modifiers #:modify). Don't
(Things start a bit slowly, but don't worry, it gets more and more interesting!)
Not very exciting so far. But
pushnew give you access to 3 modify-macros, whereas
modify gives you access to literally hundreds!
;; "string-upcasef"?... (let ((place
string-upcaseplace)) place) =>
;; "listf"? (let ((place 'atom)) (
listplace)) place) => (ATOM)
;; "parse-integerf"? (let ((place
parse-integerplace)) place) => 1986
One might wonder, why not just write this instead?
(And forget about
(nreverse (list 1 2 3)) or
(list 3 2 1) because that's missing the point. ;P) The answer is that "place" might of course be much longer-named and/or more complex than this. And of course, multiple evaluation of the place will be averted, which is important when side-effects and/or expensive accesses are involved.
(let ((hash (make-hash-table))) (setf (gethash 'key hash) 10) (
/(gethash (print 'key) hash) 5)) (gethash 'key hash)) == (let ((hash (make-hash-table))) (setf (gethash 'key hash) 10) (let ((key (print 'key))) (setf (gethash key hash) (/ (gethash key hash) 5))) (gethash 'key hash)) -| KEY => 2, T
modify normally returns the new value(s) of the place, per the usual conventions:
But one simple yet very useful feature is to be able to return the old value instead:
Some place-modifiers are also valid places. One example is
aref. In the following example, how does
modify know which of
(aref object 0) or object should be interpreted as being the place to modify?
modify is "conservative" by default, so as soon as it encounters a possible place while recursing through the "spots" (described and explained below), then it will treat that as the place. This is the most intuitive possible default and is usually what you want.
In the above example,
(aref object 0) is the place to modify, not object.
Some place-modifiers are known to
modify as being "inconceivable places", which allows conservative recursion to proceed (at least) one step further, much conveniently:
After finding the most conservative place,
modify will still speculatively recurse through the remaining "spots" in search of a
:place "local special form", which would explicitly indicate at what level lies the intended place, overriding the conservative behavior.
Of course, the "top-level" (ignoring
modify can only accept a PME and not a place, so there can be no ambiguity there:
modify can accept multiple PMEs, in which case the modifications will happen in sequence, much in the same way as
setf with multiple places.
Up to this point, we've always used the "primary variant", which is the one you'll need most often, but each place-modifier kind can have up to 4 variants, though most only have one or two. The "variant" determines which argument is treated as the "spot", positionally.
The determination of which variant maps to which spot is made by the definer of the place-modifier.
So as of version 2.1, there are 301 place-modifiers with one single variant, 172 with 2 variants, and only 37 with 3 or 4 variants.