Emacs Redux

Return to the Essence of Text Editing

A Peek at Emacs 24.4: Hash-table-keys & Hash-table-values

| Comments

While Emacs has a pretty good hash-table API, two functions were rather mysteriously absent from it - hash-table-keys and hash-table-values (which would return a list of all keys/values in a hash-table).

Many people who needed them simply defined them in their code directly (usually in terms of maphash), but that’s no longer necessary in Emacs 24.4, as they are now part of the new built-in library subr-x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
;; all functions in the library are defined as inline, so you don't
;; need to require the library at runtime
(eval-when-compile (require 'subr-x))

(setq h (make-hash-table))

(puthash "Batman" "Bruce Wayne" h)
(puthash "Spiderman" "Peter Parker" h)
(puthash "Superman" "Clark Kent" h)

(hash-table-keys h)
; => ("Batman" "Spiderman" "Superman")

(hash-table-values h)
; => ("Bruce Wayne" "Peter Parker" "Clark Kent")

Nice and simple.

Comments