Elisp: Map to Sequence

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 lambda to each vector (take 1st element)
(mapcar (lambda (x) (aref x 0)) [[1 2] [3 4] [5 6]])
;; (1 3 5)

;; apply a buildin function named 1+ to each vector element
(mapcar '1+ [0 1 2])
;; (1 2 3)

;; apply a function to list
(mapcar '1+ '(0 1 2))
;; (1 2 3)
seq-map

Apply a function over each sequence item. Return the result sequence.

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 (xe xi) (vector xe xi))
 (list 'a 'b 'c))

;; ([a 0] [b 1] [c 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