Skip to content

Side effects

Functions iterating over lists for side effect only.

Function: -each (list fn)

Call fn on each element of list. Return nil; this function is intended for side effects.

Its anaphoric counterpart is --each.

For access to the current element’s index in list, see -each-indexed (see -each-indexed).

emacs-lisp
(let (l) (-each '(1 2 3) (lambda (x) (push x l))) l)
    ⇒ (3 2 1)
emacs-lisp
(let (l) (--each '(1 2 3) (push it l)) l)
    ⇒ (3 2 1)
emacs-lisp
(-each '(1 2 3) #'identity)
nil

Function: -each-while (list pred fn)

Call fn on each item in list, while (pred item) is non-nil. Once an item is reached for which pred returns nil, fn is no longer called. Return nil; this function is intended for side effects.

Its anaphoric counterpart is --each-while.

emacs-lisp
(let (l) (-each-while '(2 4 5 6) #'even? (lambda (x) (push x l))) l)
    ⇒ (4 2)
emacs-lisp
(let (l) (--each-while '(1 2 3 4) (< it 3) (push it l)) l)
    ⇒ (2 1)
emacs-lisp
(let ((s 0)) (--each-while '(1 3 4 5) (< it 5) (setq s (+ s it))) s)
8

Function: -each-indexed (list fn)

Call fn on each index and element of list. For each item at index in list, call (funcall fn index item). Return nil; this function is intended for side effects.

See also: -map-indexed (see -map-indexed).

emacs-lisp
(let (l) (-each-indexed '(a b c) (lambda (i x) (push (list x i) l))) l)
    ⇒ ((c 2) (b 1) (a 0))
emacs-lisp
(let (l) (--each-indexed '(a b c) (push (list it it-index) l)) l)
    ⇒ ((c 2) (b 1) (a 0))
emacs-lisp
(let (l) (--each-indexed () (push it l)) l)
    ⇒ ()

Function: -each-r (list fn)

Call fn on each element of list in reversed order. Return nil; this function is intended for side effects.

Its anaphoric counterpart is --each-r.

emacs-lisp
(let (l) (-each-r '(1 2 3) (lambda (x) (push x l))) l)
    ⇒ (1 2 3)
emacs-lisp
(let (l) (--each-r '(1 2 3) (push it l)) l)
    ⇒ (1 2 3)
emacs-lisp
(-each-r '(1 2 3) #'identity)
nil

Function: -each-r-while (list pred fn)

Call fn on each item in reversed list, while (pred item) is non-nil. Once an item is reached for which pred returns nil, fn is no longer called. Return nil; this function is intended for side effects.

Its anaphoric counterpart is --each-r-while.

emacs-lisp
(let (l) (-each-r-while '(2 4 5 6) #'even? (lambda (x) (push x l))) l)
    ⇒ (6)
emacs-lisp
(let (l) (--each-r-while '(1 2 3 4) (>= it 3) (push it l)) l)
    ⇒ (3 4)
emacs-lisp
(let ((s 0)) (--each-r-while '(1 2 3 5) (> it 1) (setq s (+ s it))) s)
10

Function: -dotimes (num fn)

Call fn num times, presumably for side effects. fn is called with a single argument on successive integers running from 0, inclusive, to num, exclusive. fn is not called if num is less than 1.

This function’s anaphoric counterpart is --dotimes.

emacs-lisp
(let (s) (-dotimes 3 (lambda (n) (push n s))) s)
    ⇒ (2 1 0)
emacs-lisp
(let (s) (-dotimes 0 (lambda (n) (push n s))) s)
    ⇒ ()
emacs-lisp
(let (s) (--dotimes 5 (push it s)) s)
    ⇒ (4 3 2 1 0)