Skip to content

Sublist selection

Functions returning a sublist of the original list.

Function: -filter (pred list)

Return a new list of the items in list for which pred returns non-nil.

Alias: -select.

This function’s anaphoric counterpart is --filter.

For similar operations, see also -keep (see -keep) and -remove (see -remove).

emacs-lisp
(-filter (lambda (num) (= 0 (% num 2))) '(1 2 3 4))
    ⇒ (2 4)
emacs-lisp
(-filter #'natnump '(-2 -1 0 1 2))
    ⇒ (0 1 2)
emacs-lisp
(--filter (= 0 (% it 2)) '(1 2 3 4))
    ⇒ (2 4)

Function: -remove (pred list)

Return a new list of the items in list for which pred returns nil.

Alias: -reject.

This function’s anaphoric counterpart is --remove.

For similar operations, see also -keep (see -keep) and -filter (see -filter).

emacs-lisp
(-remove (lambda (num) (= 0 (% num 2))) '(1 2 3 4))
    ⇒ (1 3)
emacs-lisp
(-remove #'natnump '(-2 -1 0 1 2))
    ⇒ (-2 -1)
emacs-lisp
(--remove (= 0 (% it 2)) '(1 2 3 4))
    ⇒ (1 3)

Function: -remove-first (pred list)

Remove the first item from list for which pred returns non-nil. This is a non-destructive operation, but only the front of list leading up to the removed item is a copy; the rest is list’s original tail. If no item is removed, then the result is a complete copy.

Alias: -reject-first.

This function’s anaphoric counterpart is --remove-first.

See also -map-first (see -map-first), -remove-item (see -remove-item), and -remove-last (see -remove-last).

emacs-lisp
(-remove-first #'natnump '(-2 -1 0 1 2))
    ⇒ (-2 -1 1 2)
emacs-lisp
(-remove-first #'stringp '(1 2 "first" "second"))
    ⇒ (1 2 "second")
emacs-lisp
(--remove-first (> it 3) '(1 2 3 4 5 6))
    ⇒ (1 2 3 5 6)

Function: -remove-last (pred list)

Remove the last item from list for which pred returns non-nil. The result is a copy of list regardless of whether an element is removed.

Alias: -reject-last.

This function’s anaphoric counterpart is --remove-last.

See also -map-last (see -map-last), -remove-item (see -remove-item), and -remove-first (see -remove-first).

emacs-lisp
(-remove-last #'natnump '(1 3 5 4 7 8 10 -11))
    ⇒ (1 3 5 4 7 8 -11)
emacs-lisp
(-remove-last #'stringp '(1 2 "last" "second"))
    ⇒ (1 2 "last")
emacs-lisp
(--remove-last (> it 3) '(1 2 3 4 5 6 7 8 9 10))
    ⇒ (1 2 3 4 5 6 7 8 9)

Function: -remove-item (item list)

Return a copy of list with all occurrences of item removed. The comparison is done with equal.

emacs-lisp
(-remove-item 3 '(1 2 3 2 3 4 5 3))
    ⇒ (1 2 2 4 5)
emacs-lisp
(-remove-item 'foo '(foo bar baz foo))
    ⇒ (bar baz)
emacs-lisp
(-remove-item "bob" '("alice" "bob" "eve" "bob"))
    ⇒ ("alice" "eve")

Function: -non-nil (list)

Return a copy of list with all nil items removed.

emacs-lisp
(-non-nil '(nil 1 nil 2 nil nil 3 4 nil 5 nil))
    ⇒ (1 2 3 4 5)
emacs-lisp
(-non-nil '((nil)))
    ⇒ ((nil))
emacs-lisp
(-non-nil ())
    ⇒ ()

Function: -slice (list from &optional to step)

Return copy of list, starting from index from to index to.

from or to may be negative. These values are then interpreted modulo the length of the list.

If step is a number, only each STEPth item in the resulting section is returned. Defaults to 1.

emacs-lisp
(-slice '(1 2 3 4 5) 1)
    ⇒ (2 3 4 5)
emacs-lisp
(-slice '(1 2 3 4 5) 0 3)
    ⇒ (1 2 3)
emacs-lisp
(-slice '(1 2 3 4 5 6 7 8 9) 1 -1 2)
    ⇒ (2 4 6 8)

Function: -take (n list)

Return a copy of the first n items in list. Return a copy of list if it contains n items or fewer. Return nil if n is zero or less.

See also: -take-last (see -take-last).

emacs-lisp
(-take 3 '(1 2 3 4 5))
    ⇒ (1 2 3)
emacs-lisp
(-take 17 '(1 2 3 4 5))
    ⇒ (1 2 3 4 5)
emacs-lisp
(-take 0 '(1 2 3 4 5))
    ⇒ ()

Function: -take-last (n list)

Return a copy of the last n items of list in order. Return a copy of list if it contains n items or fewer. Return nil if n is zero or less.

See also: -take (see -take).

emacs-lisp
(-take-last 3 '(1 2 3 4 5))
    ⇒ (3 4 5)
emacs-lisp
(-take-last 17 '(1 2 3 4 5))
    ⇒ (1 2 3 4 5)
emacs-lisp
(-take-last 1 '(1 2 3 4 5))
    ⇒ (5)

Function: -drop (n list)

Return the tail (not a copy) of list without the first n items. Return nil if list contains n items or fewer. Return list if n is zero or less.

For another variant, see also -drop-last (see -drop-last).

emacs-lisp
(-drop 3 '(1 2 3 4 5))
    ⇒ (4 5)
emacs-lisp
(-drop 17 '(1 2 3 4 5))
    ⇒ ()
emacs-lisp
(-drop 0 '(1 2 3 4 5))
    ⇒ (1 2 3 4 5)

Function: -drop-last (n list)

Return a copy of list without its last n items. Return a copy of list if n is zero or less. Return nil if list contains n items or fewer.

See also: -drop (see -drop).

emacs-lisp
(-drop-last 3 '(1 2 3 4 5))
    ⇒ (1 2)
emacs-lisp
(-drop-last 17 '(1 2 3 4 5))
    ⇒ ()
emacs-lisp
(-drop-last 0 '(1 2 3 4 5))
    ⇒ (1 2 3 4 5)

Function: -take-while (pred list)

Take successive items from list for which pred returns non-nil. pred is a function of one argument. Return a new list of the successive elements from the start of list for which pred returns non-nil.

This function’s anaphoric counterpart is --take-while.

For another variant, see also -drop-while (see -drop-while).

emacs-lisp
(-take-while #'even? '(1 2 3 4))
    ⇒ ()
emacs-lisp
(-take-while #'even? '(2 4 5 6))
    ⇒ (2 4)
emacs-lisp
(--take-while (< it 4) '(1 2 3 4 3 2 1))
    ⇒ (1 2 3)

Function: -drop-while (pred list)

Drop successive items from list for which pred returns non-nil. pred is a function of one argument. Return the tail (not a copy) of list starting from its first element for which pred returns nil.

This function’s anaphoric counterpart is --drop-while.

For another variant, see also -take-while (see -take-while).

emacs-lisp
(-drop-while #'even? '(1 2 3 4))
    ⇒ (1 2 3 4)
emacs-lisp
(-drop-while #'even? '(2 4 5 6))
    ⇒ (5 6)
emacs-lisp
(--drop-while (< it 4) '(1 2 3 4 3 2 1))
    ⇒ (4 3 2 1)

Function: -select-by-indices (indices list)

Return a list whose elements are elements from list selected as ‘(nth i list)‘ for all i from indices.

emacs-lisp
(-select-by-indices '(4 10 2 3 6) '("v" "e" "l" "o" "c" "i" "r" "a" "p" "t" "o" "r"))
    ⇒ ("c" "o" "l" "o" "r")
emacs-lisp
(-select-by-indices '(2 1 0) '("a" "b" "c"))
    ⇒ ("c" "b" "a")
emacs-lisp
(-select-by-indices '(0 1 2 0 1 3 3 1) '("f" "a" "r" "l"))
    ⇒ ("f" "a" "r" "f" "a" "l" "l" "a")

Function: -select-columns (columns table)

Select columns from table.

table is a list of lists where each element represents one row. It is assumed each row has the same length.

Each row is transformed such that only the specified columns are selected.

See also: -select-column (see -select-column), -select-by-indices (see -select-by-indices)

emacs-lisp
(-select-columns '(0 2) '((1 2 3) (a b c) (:a :b :c)))
    ⇒ ((1 3) (a c) (:a :c))
emacs-lisp
(-select-columns '(1) '((1 2 3) (a b c) (:a :b :c)))
    ⇒ ((2) (b) (:b))
emacs-lisp
(-select-columns nil '((1 2 3) (a b c) (:a :b :c)))
    ⇒ (nil nil nil)

Function: -select-column (column table)

Select column from table.

table is a list of lists where each element represents one row. It is assumed each row has the same length.

The single selected column is returned as a list.

See also: -select-columns (see -select-columns), -select-by-indices (see -select-by-indices)

emacs-lisp
(-select-column 1 '((1 2 3) (a b c) (:a :b :c)))
    ⇒ (2 b :b)