Elisp: Generic Map Functions (for hash table, alist, plist)

By Xah Lee. Date: . Last updated: .

What is Generic Map Functions

to use them, you need to load the library.

(require 'map)

They are not documentated in emacs lisp manual as of 2025-05-20.

Apply Function to Key Val Pairs

map-apply

(map-apply FUNCTION MAP)

  • Apply FUNCTION to each element of MAP
  • Return the result as a list.
  • FUNCTION is called with two arguments, the key and value.
(map-apply
 (lambda (xkey xvalue) (format "%s,%s" xkey xvalue))
 (list
  (cons 1 2)
  (cons 3 4)
  (cons 5 6)))
;; ("1,2" "3,4" "5,6")

Filter

map-filter

(map-filter PRED MAP)

  • Filter a MAP by function PRED.
  • If PRED return true, the item is kept.
  • The function PRED takes 2 args key val
  • Return an Association List
;; make hash table
(progn
  (setq xx (make-hash-table :test 'equal))
  (puthash "joe" 19 xx)
  (puthash "jane" 20 xx)
  (puthash "liz" 21 xx))

(map-filter
 (lambda (_ val)
   (if (eq 0 (mod val 2))
       t
     nil))
 xx)
;; (("jane" . 20))

Convert to Association List

map-pairs

(map-pairs MAP)

Return the key value pairs in MAP as an alist.

;; make hash table
(progn
  (setq xx (make-hash-table :test 'equal))
  (puthash "joe" 19 xx)
  (puthash "jane" 20 xx)
  (puthash "liz" 21 xx))

;; convert to alist
(map-pairs xx)
;; (("joe" . 19) ("jane" . 20) ("liz" . 21))

Convert to hashtable

map-into

(map-into MAP TYPE)

Convert MAP into a hash-table.

TYPE is a list whose car is symbol hash-table and cdr a list of keyword-args forwarded to make-hash-table. e.g. '(hash-table :test equal)

;; convert alist to hashtable. key as string
(map-into '(("joe" . 3) ("jane" . 4)) '(hash-table :test equal))
;; #s(hash-table test equal data ("joe" 3 "jane" 4))

;; convert alist to hashtable. key as number
(map-into '((1 . 3) (2 . 4)) '(hash-table :test eql))
;; #s(hash-table data (1 3 2 4))

;; convert list to hashtable. key as string
(map-into '(("joe" 3) ("jane" 4)) '(hash-table :test equal))
;; #s(hash-table test equal data ("joe" (3) "jane" (4)))

;; convert property list to hashtable. key as symbol
(map-into '(:joe 3 :jane 4) '(hash-table :test eq))
;; #s(hash-table test eq data (:joe 3 :jane 4))

more

xtodo

Elisp, key value collection

Elisp, Hash Table