ELisp: Map, Iteration
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)
mapc (for-each)
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"))
dolist: Loop Thru a List
dolist
-
(dolist (VAR LIST) BODY)
(dolist (VAR LIST RESULT) BODY)
- Loop over a list.
- Each time eval BODY, with VAR having value of an element in list.
- Return nil or RESULT.
- RESULT is evaluated last.
(let (xx) (setq xx (number-sequence 1 5)) (dolist (n xx) (insert (number-to-string n)))) ;; inserts 12345 ;; return nil
Loop Thru a Sequence
to loop thru a sequence, use
seq-doseq
(setq xx [1 2 3] ) (seq-doseq (x xx) (message "x is %s" x)) ;; return the original sequence [1 2 3]