@std/collections/itertools.ms#
Import each required symbol with import Name from "@std/collections/itertools.ms".
Run Type#
A run of consecutive items that share a key, as returned by group_runs.
type Run[K, T] {
key K
items List[T]
}Run.items#
The items of the run in their original order.
items List[T]Run.key#
The key shared by every item in the run.
key Kaccumulate Function#
Returns the running results of folding step over the items: the first item, then step(first, second), and so on.
The result has as many items as the input, so an empty list gives an empty list.
Example: accumulate([1, 2, 3, 4], (a, b) => a + b) is [1, 3, 6, 10].
fn accumulate[T Clonable](values List[T], step fn(T, T) T) List[T] { ... }combinations Function#
Returns every selection of size items, each in its original order, in lexicographic order of position. Items are told apart by position, not value.
A size outside 0 through the list length has no selections and gives an empty list.
Example: combinations([1, 2, 3], 2) is [[1, 2], [1, 3], [2, 3]].
fn combinations[T Clonable](values List[T], size Int) List[List[T]] { ... }group_runs Function#
Splits the list into runs of consecutive items with equal keys. key is called once per item, in order.
Items with the same key that are not adjacent form separate runs; sort first to gather them. An empty list gives an empty list.
Example: group_runs([1, 1, 2, 1], n => n) has the runs 1 1, 2 and 1.
fn group_runs[T Clonable, K Equal & Clonable](values List[T], key fn(T) K) List[Run[K, T]] { ... }pairwise Function#
Pairs each item with the one after it; a list with fewer than two items gives an
empty list. Example: pairwise([1, 2, 3]) holds the pairs (1, 2) and (2, 3).
fn pairwise[T Clonable](values List[T]) List[Pair[T, T]] { ... }permutations Function#
Returns every ordering of size items chosen from the list, in lexicographic order of position. Items are told apart by position, not value.
Omit size for full-length orderings. A size outside 0 through the list length has no orderings and gives an empty list.
Example: permutations([1, 2, 3], 2) is [[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]].
fn permutations[T Clonable](values List[T], size Int? = null) List[List[T]] { ... }product Function#
Returns the Cartesian product: one list for each way of taking one item from each input list, in lexicographic order with the last list varying fastest.
No input lists gives one empty combination; any empty input list gives an empty result.
Example: product([[1, 2], [3, 4]]) is [[1, 3], [1, 4], [2, 3], [2, 4]].
fn product[T Clonable](lists List[List[T]]) List[List[T]] { ... }zip_longest Function#
Pairs items by position and pads the shorter list with null, so the result is as long
as the longer list. Example: zip_longest([1, 2, 3], ["a"]) pairs (1, "a"),
(2, null) and (3, null).
fn zip_longest[A Clonable, B Clonable](left List[A], right List[B]) List[Pair[A?, B?]] { ... }