Elisp: Sequence. Map

By Xah Lee. Date: . Last updated: .

Map a Function to Sequence

mapcar

(mapcar FUNCTION SEQUENCE)

  • Apply function to each element of Sequence
  • Return the result as a list.
  • FUNCTION must be a Symbol, or Lambda.
;; apply a function to list
(mapcar '1+ '(0 1 2))
;; (1 2 3)
;; apply a function to each vector element
(mapcar '1+ [0 1 2])
;; (1 2 3)
;; apply a lambda to each vector (take 1st element)
(mapcar (lambda (x) (aref x 0)) [[1 2] [3 4] [5 6]])
;; (1 3 5)
seq-map

similar to mapcar, but can work on any new sequence type.

Map with Index

seq-map-indexed

(seq-map-indexed FUNCTION SEQUENCE)

Apply a function to sequence, with index.

The function is given 2 args, the element and its index.

(seq-map-indexed
 (lambda (xitem xindex) (vector xitem xindex))
 (vector 33 44 55))
;; ([33 0] [44 1] [55 2])

Map a Function of N Args to N Sequences

seq-mapn
  • Apply a function of n params over n sequences, taking one arg from each.
  • return a list.
(seq-mapn
 (lambda (x y) (+ x y))
 (vector 1 2 3)
 (vector 4 5 6))
;; (5 7 9)

Elisp, sequence functions