This is part 4 in my terse hashes in emacs lisp series
- Terse Hashes in Emacs Lisp
- Data::Dumper in Emacs Lisp
- Terse Hash Dereferencing
- Terse Hashes Miscellany
Okay, time to wrap up with the terse hashes.
The built-in hash tables have equivalents to the perl exists and delete, but again, the argument order does not feel right to me.
Having looked at ruby a bit over the past few months, I’ve decided I like predicates ending with a question mark1 rather than a p.
(defsubst exists? (hash key) (not (eq (gethash key hash 'missing-key) 'missing-key))) (defsubst del-hash! (hash key) (remhash key hash))
And the other thing I rely on a lot in perl is keys so I can choose the traversal order with sort.
(defun keys (hash) (let (keys) (maphash (lambda (k v) (push k keys)) hash) keys))
I need a decent less than method
(defun generic/less (val1 val2) (if (and (numberp val1) (numberp val2)) (< val1 val2) (let ((v1 (prin1-to-string val1)) (v2 (prin1-to-string val2))) (string< v1 v2))))
and then this sort of thing just works
(defvar %hash (_h { 1 2 'a 'x 5 6 10 "lemon" })) (sort (keys %hash) #'generic/less) ;; --> (1 5 10 a)
1. Was this first in scheme?