Skip to content

List to list

Functions returning a modified copy of the input list.

Function: -keep (fn list)

Return a new list of the non-nil results of applying fn to each item in list. Like -filter (see -filter), but returns the non-nil results of fn instead of the corresponding elements of list.

Its anaphoric counterpart is --keep.

emacs-lisp
(-keep #'cdr '((1 2 3) (4 5) (6)))
    ⇒ ((2 3) (5))
emacs-lisp
(-keep (lambda (n) (and (> n 3) (* 10 n))) '(1 2 3 4 5 6))
    ⇒ (40 50 60)
emacs-lisp
(--keep (and (> it 3) (* 10 it)) '(1 2 3 4 5 6))
    ⇒ (40 50 60)

Function: -concat (&rest sequences)

Concatenate all sequences and make the result a list. The result is a list whose elements are the elements of all the arguments. Each argument may be a list, vector or string.

All arguments except the last argument are copied. The last argument is just used as the tail of the new list. If the last argument is not a list, this results in a dotted list.

As an exception, if all the arguments except the last are nil, and the last argument is not a list, the return value is that last argument unaltered, not a list.

emacs-lisp
(-concat '(1))
    ⇒ (1)
emacs-lisp
(-concat '(1) '(2))
    ⇒ (1 2)
emacs-lisp
(-concat '(1) '(2 3) '(4))
    ⇒ (1 2 3 4)

Function: -flatten (l)

Take a nested list l and return its contents as a single, flat list.

Note that because nil represents a list of zero elements (an empty list), any mention of nil in l will disappear after flattening. If you need to preserve nils, consider -flatten-n (see -flatten-n) or map them to some unique symbol and then map them back.

Conses of two atoms are considered "terminals", that is, they aren’t flattened further.

See also: -flatten-n (see -flatten-n)

emacs-lisp
(-flatten '((1)))
    ⇒ (1)
emacs-lisp
(-flatten '((1 (2 3) (((4 (5)))))))
    ⇒ (1 2 3 4 5)
emacs-lisp
(-flatten '(1 2 (3 . 4)))
    ⇒ (1 2 (3 . 4))

Function: -flatten-n (num list)

Flatten num levels of a nested list.

See also: -flatten (see -flatten)

emacs-lisp
(-flatten-n 1 '((1 2) ((3 4) ((5 6)))))
    ⇒ (1 2 (3 4) ((5 6)))
emacs-lisp
(-flatten-n 2 '((1 2) ((3 4) ((5 6)))))
    ⇒ (1 2 3 4 (5 6))
emacs-lisp
(-flatten-n 3 '((1 2) ((3 4) ((5 6)))))
    ⇒ (1 2 3 4 5 6)

Function: -replace (old new list)

Replace all old items in list with new.

Elements are compared using equal.

See also: -replace-at (see -replace-at)

emacs-lisp
(-replace 1 "1" '(1 2 3 4 3 2 1))
    ⇒ ("1" 2 3 4 3 2 "1")
emacs-lisp
(-replace "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo"))
    ⇒ ("a" "nice" "bar" "sentence" "about" "bar")
emacs-lisp
(-replace 1 2 nil)
nil

Function: -replace-first (old new list)

Replace the first occurrence of old with new in list.

Elements are compared using equal.

See also: -map-first (see -map-first)

emacs-lisp
(-replace-first 1 "1" '(1 2 3 4 3 2 1))
    ⇒ ("1" 2 3 4 3 2 1)
emacs-lisp
(-replace-first "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo"))
    ⇒ ("a" "nice" "bar" "sentence" "about" "foo")
emacs-lisp
(-replace-first 1 2 nil)
nil

Function: -replace-last (old new list)

Replace the last occurrence of old with new in list.

Elements are compared using equal.

See also: -map-last (see -map-last)

emacs-lisp
(-replace-last 1 "1" '(1 2 3 4 3 2 1))
    ⇒ (1 2 3 4 3 2 "1")
emacs-lisp
(-replace-last "foo" "bar" '("a" "nice" "foo" "sentence" "about" "foo"))
    ⇒ ("a" "nice" "foo" "sentence" "about" "bar")
emacs-lisp
(-replace-last 1 2 nil)
nil

Function: -insert-at (n x list)

Return a list with x inserted into list at position n.

See also: -splice (see -splice), -splice-list (see -splice-list)

emacs-lisp
(-insert-at 1 'x '(a b c))
    ⇒ (a x b c)
emacs-lisp
(-insert-at 12 'x '(a b c))
    ⇒ (a b c x)

Function: -replace-at (n x list)

Return a list with element at Nth position in list replaced with x.

See also: -replace (see -replace)

emacs-lisp
(-replace-at 0 9 '(0 1 2 3 4 5))
    ⇒ (9 1 2 3 4 5)
emacs-lisp
(-replace-at 1 9 '(0 1 2 3 4 5))
    ⇒ (0 9 2 3 4 5)
emacs-lisp
(-replace-at 4 9 '(0 1 2 3 4 5))
    ⇒ (0 1 2 3 9 5)

Function: -update-at (n func list)

Use func to update the Nth element of list. Return a copy of list where the Nth element is replaced with the result of calling func on it.

See also: -map-when (see -map-when)

emacs-lisp
(-update-at 0 (lambda (x) (+ x 9)) '(0 1 2 3 4 5))
    ⇒ (9 1 2 3 4 5)
emacs-lisp
(-update-at 1 (lambda (x) (+ x 8)) '(0 1 2 3 4 5))
    ⇒ (0 9 2 3 4 5)
emacs-lisp
(--update-at 2 (length it) '("foo" "bar" "baz" "quux"))
    ⇒ ("foo" "bar" 3 "quux")

Function: -remove-at (n list)

Return list with its element at index n removed. That is, remove any element selected as (nth n list) from list and return the result.

This is a non-destructive operation: parts of list (but not necessarily all of it) are copied as needed to avoid destructively modifying it.

See also: -remove-at-indices (see -remove-at-indices), -remove (see -remove).

emacs-lisp
(-remove-at 0 '(a b c))
    ⇒ (b c)
emacs-lisp
(-remove-at 1 '(a b c))
    ⇒ (a c)
emacs-lisp
(-remove-at 2 '(a b c))
    ⇒ (a b)

Function: -remove-at-indices (indices list)

Return list with its elements at indices removed. That is, for each index i in indices, remove any element selected as (nth i list) from list.

This is a non-destructive operation: parts of list (but not necessarily all of it) are copied as needed to avoid destructively modifying it.

See also: -remove-at (see -remove-at), -remove (see -remove).

emacs-lisp
(-remove-at-indices '(0) '(a b c d e))
    ⇒ (b c d e)
emacs-lisp
(-remove-at-indices '(1 3) '(a b c d e))
    ⇒ (a c e)
emacs-lisp
(-remove-at-indices '(4 0 2) '(a b c d e))
    ⇒ (b d)