6 ms·
Don't know if this will do what you want, but these two reader macros add syntactic sugar for Common Lisp's aref and gethash forms. You should put these definit
by gcv 18y ago
Don't know if this will do what you want, but these two reader macros add syntactic sugar for Common Lisp's aref and gethash forms. You should put these definitions in an eval-when. Disclaimer: I haven't used these in a while (not programming in Lisp at the moment, alas), so they may have some limitations which I have forgotten.
(set-macro-character
#\] (get-macro-character #\)))
;; Array syntax: [array-name array-references] is equivalent to
;; (aref array-name array-references)
(set-macro-character
#\[
#'(lambda (stream char)
(declare (ignore char))
(destructuring-bind (array-name &rest array-references)
(read-delimited-list #\] stream t)
`(aref ,array-name ,@array-references))))
;; Hash table syntax: #[hash-table hash-key &optional default] is
;; equivalent to (gethash hash-key hash-table &optional default)
(set-dispatch-macro-character
#\# #\[ #'(lambda (stream subchar arg)
(declare (ignore subchar arg))
(destructuring-bind (hash-name hash-key &optional (default nil))
(read-delimited-list #\] stream t)
`(gethash ,hash-key ,hash-name ,default))))
- silentbicycle 18y agoActually, would there be any real problem with keeping the map in a closure, and passing around a function that gets (or, with a second optional argument, sets) the value for a given key? (h 'key) and (h 'key 'new-val) That's not too bad syntax-wise. Or, taking a cue from actor languages: (h 'get 'key) and (h 'set 'key 'new-val)