all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: emacs-devel@gnu.org
Subject: Re: RFC: make maphash return a list
Date: Sat, 04 May 2013 01:12:58 +0200	[thread overview]
Message-ID: <87txmjwtc5.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: CAFXAjY72zDAGLnZro9MkqgRDa_eyEu9fvufu_rc9=8HGcocNmg@mail.gmail.com

Wilfred Hughes <me@wilfred.me.uk> 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 {}.




  reply	other threads:[~2013-05-03 23:12 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-03 17:59 RFC: make maphash return a list Wilfred Hughes
2013-05-03 23:12 ` Pascal J. Bourguignon [this message]
2013-05-04  5:29 ` Stephen J. Turnbull
2013-05-05  0:06   ` Wilfred Hughes
2013-06-18  8:17     ` Klaus-Dieter Bauer
2013-06-18 15:11       ` Davis Herring
2013-06-18 20:06         ` Klaus-Dieter Bauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87txmjwtc5.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.