Skip to content

Partitioning

Functions partitioning the input list into a list of lists.

Function: -split-at (n list)

Split list into two sublists after the Nth element. The result is a list of two elements (take drop) where take is a new list of the first n elements of list, and drop is the remaining elements of list (not a copy). take and drop are like the results of -take (see -take) and -drop (see -drop), respectively, but the split is done in a single list traversal.

emacs-lisp
(-split-at 3 '(1 2 3 4 5))
    ⇒ ((1 2 3) (4 5))
emacs-lisp
(-split-at 17 '(1 2 3 4 5))
    ⇒ ((1 2 3 4 5) nil)
emacs-lisp
(-split-at 0 '(1 2 3 4 5))
    ⇒ (nil (1 2 3 4 5))

Function: -split-with (pred list)

Split list into a prefix satisfying pred, and the rest. The first sublist is the prefix of list with successive elements satisfying pred, and the second sublist is the remaining elements that do not. The result is like performing

((-take-while pred list) (-drop-while pred list))

but in no more than a single pass through list.

emacs-lisp
(-split-with 'even? '(1 2 3 4))
    ⇒ (nil (1 2 3 4))
emacs-lisp
(-split-with 'even? '(2 4 5 6))
    ⇒ ((2 4) (5 6))
emacs-lisp
(--split-with (< it 4) '(1 2 3 4 3 2 1))
    ⇒ ((1 2 3) (4 3 2 1))

Macro: -split-on (item list)

Split the list each time item is found.

Unlike -partition-by (see -partition-by), the item is discarded from the results. Empty lists are also removed from the result.

Comparison is done by equal.

See also -split-when (see -split-when)

emacs-lisp
(-split-on '| '(Nil | Leaf a | Node [Tree a]))
    ⇒ ((Nil) (Leaf a) (Node [Tree a]))
emacs-lisp
(-split-on :endgroup '("a" "b" :endgroup "c" :endgroup "d" "e"))
    ⇒ (("a" "b") ("c") ("d" "e"))
emacs-lisp
(-split-on :endgroup '("a" "b" :endgroup :endgroup "d" "e"))
    ⇒ (("a" "b") ("d" "e"))

Function: -split-when (fn list)

Split the list on each element where fn returns non-nil.

Unlike -partition-by (see -partition-by), the "matched" element is discarded from the results. Empty lists are also removed from the result.

This function can be thought of as a generalization of split-string.

emacs-lisp
(-split-when 'even? '(1 2 3 4 5 6))
    ⇒ ((1) (3) (5))
emacs-lisp
(-split-when 'even? '(1 2 3 4 6 8 9))
    ⇒ ((1) (3) (9))
emacs-lisp
(--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest args))
    ⇒ ((a b) (c d) (args))

Function: -separate (pred list)

Split list into two sublists based on whether items satisfy pred. The result is like performing

((-filter pred list) (-remove pred list))

but in a single pass through list.

emacs-lisp
(-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7))
    ⇒ ((2 4 6) (1 3 5 7))
emacs-lisp
(--separate (< it 5) '(3 7 5 9 3 2 1 4 6))
    ⇒ ((3 3 2 1 4) (7 5 9 6))
emacs-lisp
(-separate 'cdr '((1 2) (1) (1 2 3) (4)))
    ⇒ (((1 2) (1 2 3)) ((1) (4)))

Function: -partition (n list)

Return a new list with the items in list grouped into n-sized sublists. If there are not enough items to make the last group n-sized, those items are discarded.

emacs-lisp
(-partition 2 '(1 2 3 4 5 6))
    ⇒ ((1 2) (3 4) (5 6))
emacs-lisp
(-partition 2 '(1 2 3 4 5 6 7))
    ⇒ ((1 2) (3 4) (5 6))
emacs-lisp
(-partition 3 '(1 2 3 4 5 6 7))
    ⇒ ((1 2 3) (4 5 6))

Function: -partition-all (n list)

Return a new list with the items in list grouped into n-sized sublists. The last group may contain less than n items.

emacs-lisp
(-partition-all 2 '(1 2 3 4 5 6))
    ⇒ ((1 2) (3 4) (5 6))
emacs-lisp
(-partition-all 2 '(1 2 3 4 5 6 7))
    ⇒ ((1 2) (3 4) (5 6) (7))
emacs-lisp
(-partition-all 3 '(1 2 3 4 5 6 7))
    ⇒ ((1 2 3) (4 5 6) (7))

Function: -partition-in-steps (n step list)

Partition list into sublists of length n that are step items apart. Like -partition-all-in-steps (see -partition-all-in-steps), but if there are not enough items to make the last group n-sized, those items are discarded.

emacs-lisp
(-partition-in-steps 2 1 '(1 2 3 4))
    ⇒ ((1 2) (2 3) (3 4))
emacs-lisp
(-partition-in-steps 3 2 '(1 2 3 4))
    ⇒ ((1 2 3))
emacs-lisp
(-partition-in-steps 3 2 '(1 2 3 4 5))
    ⇒ ((1 2 3) (3 4 5))

Function: -partition-all-in-steps (n step list)

Partition list into sublists of length n that are step items apart. Adjacent groups may overlap if n exceeds the step stride. Trailing groups may contain less than n items.

emacs-lisp
(-partition-all-in-steps 2 1 '(1 2 3 4))
    ⇒ ((1 2) (2 3) (3 4) (4))
emacs-lisp
(-partition-all-in-steps 3 2 '(1 2 3 4))
    ⇒ ((1 2 3) (3 4))
emacs-lisp
(-partition-all-in-steps 3 2 '(1 2 3 4 5))
    ⇒ ((1 2 3) (3 4 5) (5))

Function: -partition-by (fn list)

Apply fn to each item in list, splitting it each time fn returns a new value.

emacs-lisp
(-partition-by 'even? ())
    ⇒ ()
emacs-lisp
(-partition-by 'even? '(1 1 2 2 2 3 4 6 8))
    ⇒ ((1 1) (2 2 2) (3) (4 6 8))
emacs-lisp
(--partition-by (< it 3) '(1 2 3 4 3 2 1))
    ⇒ ((1 2) (3 4 3) (2 1))

Function: -partition-by-header (fn list)

Apply fn to the first item in list. That is the header value. Apply fn to each item in list, splitting it each time fn returns the header value, but only after seeing at least one other value (the body).

emacs-lisp
(--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4))
    ⇒ ((1 2 3) (1 2) (1 2 3 4))
emacs-lisp
(--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0))
    ⇒ ((1 2 0) (1 0) (1 2 3 0))
emacs-lisp
(-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1))
    ⇒ ((2 1 1 1) (4 1 3 5) (6 6 1))

Function: -partition-after-pred (pred list)

Partition list after each element for which pred returns non-nil.

This function’s anaphoric counterpart is --partition-after-pred.

emacs-lisp
(-partition-after-pred #'booleanp ())
    ⇒ ()
emacs-lisp
(-partition-after-pred #'booleanp '(t t))
    ⇒ ((t) (t))
emacs-lisp
(-partition-after-pred #'booleanp '(0 0 t t 0 t))
    ⇒ ((0 0 t) (t) (0 t))

Function: -partition-before-pred (pred list)

Partition directly before each time pred is true on an element of list.

emacs-lisp
(-partition-before-pred #'booleanp ())
    ⇒ ()
emacs-lisp
(-partition-before-pred #'booleanp '(0 t))
    ⇒ ((0) (t))
emacs-lisp
(-partition-before-pred #'booleanp '(0 0 t 0 t t))
    ⇒ ((0 0) (t 0) (t) (t))

Function: -partition-before-item (item list)

Partition directly before each time item appears in list.

emacs-lisp
(-partition-before-item 3 ())
    ⇒ ()
emacs-lisp
(-partition-before-item 3 '(1))
    ⇒ ((1))
emacs-lisp
(-partition-before-item 3 '(3))
    ⇒ ((3))

Function: -partition-after-item (item list)

Partition directly after each time item appears in list.

emacs-lisp
(-partition-after-item 3 ())
    ⇒ ()
emacs-lisp
(-partition-after-item 3 '(1))
    ⇒ ((1))
emacs-lisp
(-partition-after-item 3 '(3))
    ⇒ ((3))

Function: -group-by (fn list)

Separate list into an alist whose keys are fn applied to the elements of list. Keys are compared by equal.

emacs-lisp
(-group-by 'even? ())
    ⇒ ()
emacs-lisp
(-group-by 'even? '(1 1 2 2 2 3 4 6 8))
    ⇒ ((nil 1 1 3) (t 2 2 2 4 6 8))
emacs-lisp
(--group-by (car (split-string it "/")) '("a/b" "c/d" "a/e"))
    ⇒ (("a" "a/b" "a/e") ("c" "c/d"))