Elisp: Sequence. Foreach (mapc, seq-do, seq-doseq)

By Xah Lee. Date: . Last updated: .

Foreach (Side-Effect, Do Not Care Result)

mapc

(mapc FUNCTION SEQUENCE)

  • Apply function to each element of Sequence
  • Return SEQUENCE unchanged.
(mapc
 (lambda (x)
   (insert (number-to-string x)))
 (number-sequence 1 9))

;; insert 123456789
;; apply a file function to a list of files
(mapc 'my-update-footer
      (list "~/x1.html" "~/x2.html" "~/x3.html"))
seq-do (alias seq-each)

Map a function over the sequence. (similar to mapc)

seq-doseq

(seq-doseq (VAR SEQUENCE) BODY)

Loop a variable over the sequence. (similar to dolist but work with Sequence Type.)

(seq-doseq (x (vector 1 2 3))
  (message "x is %s" x))

;; prints
;; x is 1
;; x is 2
;; x is 3

;; return
;; [1 2 3]

Elisp, sequence functions