[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: RFC: make maphash return a list
From: |
Pascal J. Bourguignon |
Subject: |
Re: RFC: make maphash return a list |
Date: |
Sat, 04 May 2013 01:12:58 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Wilfred Hughes <address@hidden> writes:
> I've been using Emacs hash tables a lot recently, and I would find
> several operations much easier if maphash returned a list. For
> example, I'd like to be able to do:
>
> (maphash (lambda (key value) key) some-table)
>
> to obtain the list of keys present in some-table. I would guess elisp
> is influenced by Common Lisp here, where maphash returns nil
> unconditionally. However, unlike elisp, Common Lisp's loop macro
> supports iterating over keys directly with (loop for key being
> hash-key of some-table ...) which helps considerably.
>
> I can't see any obvious way this would break backwards compatibility,
> so I threw together a trivial patch that makes maphash return a list
> of the results. I've attached the patch.
>
> Is there interest in this?
maphash is nice because it doesn't cons a list.
(setq lexical-binding t)
(defun hashmap (fun hash)
"Returns a list of the results of fun called on each key value entry.
No defined order."
(let ((result '()))
(maphash (lambda (k v) (push (funcall fun k v) result)) hash)
result))
(let ((h (make-hash-table)))
(setf (gethash :u h) 1
(gethash :d h) 2
(gethash :t h) 3
(gethash :q h) 4
(gethash :c h) 5
(gethash :s h) 6)
(hashmap (function cons) h))
--> ((:s . 6) (:c . 5) (:q . 4) (:t . 3) (:d . 2) (:u . 1))
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.