sequtils
nimony/lib/std/sequtils.nim
Operations on sequences (and openArrays), in the spirit of functional programming: map, filter, zip, folds and friends.
This is the Nimony port. It currently covers the callback- and value-oriented core.
func repeat(x: T; n: int64): seq[T]Returns a sequence with
xrepeatedntimes.func concat(a: openArray[T]; b: openArray[T]): seq[T]Concatenates
aandbinto a fresh sequence.func count(s: openArray[T]; x: T): int64Returns the number of occurrences of
xins.func deduplicate(s: openArray[T]): seq[T]Returns
swith consecutive and non-consecutive duplicates removed, preserving first-seen order.func minIndex(s: openArray[T]): int64Returns the index of the minimum element of
s(0 for an emptys).func maxIndex(s: openArray[T]): int64Returns the index of the maximum element of
s(0 for an emptys).proc map(s: openArray[T]; op: proc (x: T): S): seq[S]Returns a new sequence with
opapplied to every element ofs.proc filter(s: openArray[T]; pred: proc (x: T): bool): seq[T]Returns the elements of
sfor whichpredreturns true.proc keepIf(s: var seq[T]; pred: proc (x: T): bool)In-place variant of
filter: keeps only the elements for whichpredreturns true.proc all(s: openArray[T]; pred: proc (x: T): bool): boolWhether
predis true for every element (true for an emptys).proc any(s: openArray[T]; pred: proc (x: T): bool): boolWhether
predis true for at least one element (false for an emptys).proc apply(s: var seq[T]; op: proc (x: T): T)Applies
opto every element ofsin place.func cycle(s: openArray[T]; n: int64): seq[T]Returns a sequence with the elements of
srepeatedntimes.func zip(a: openArray[S]; b: openArray[T]): seq[tuple[S, T]]Pairs up the elements of
aandb, truncating to the shorter length.func unzip(s: openArray[tuple[S, T]]): tuple[seq[S], seq[T]]Splits a sequence of pairs into a pair of sequences.
func minmax(x: openArray[T]): tuple[T, T]Returns the
(minimum, maximum)ofxin a single pass. Requires a non-emptyx.func delete(s: var seq[T]; first: int64; last: int64)Deletes the elements
s[first..last](inclusive) in place, without building a fresh sequence: the survivors afterlastare swapped down over the gap, which leaves the dropped elements in the tail, andshrinkthen destroys exactly those dropped elements.func insert(dest: var seq[T]; src: openArray[T]; pos: int64)Inserts the elements of
srcintodestat positionpos, in place: the storage is grown once and the tail shifted up, rather than materialising a whole new sequence.template toSeq(iter: untyped): untypedMaterializes any iterable (a collection, a range, an iterator call, …) into a
seq. Example:toSeq(1 .. 3) == @[1, 2, 3];toSeq("ab") == @['a', 'b'].template foldl(s: untyped; operation: untyped): untypedLeft-associative fold.
ais the accumulator (seeded with the first element),bthe current element.template foldr(s: untyped; operation: untyped): untypedRight-associative fold.
ais the current element,bthe accumulator (seeded with the last element).template anyIt(s: untyped; pred: untyped): boolWhether
pred(referencing the injectedit) holds for any element.template allIt(s: untyped; pred: untyped): boolWhether
pred(referencing the injectedit) holds for every element.template countIt(s: untyped; pred: untyped): int64Number of elements for which
pred(referencing the injectedit) holds.template mapIt(s: untyped; op: untyped): untypedReturns a new sequence with
op(referencing the injectedit) applied to every element. The result element type istypeof(op). Example:mapIt(@[1, 2, 3], it * 10) == @[10, 20, 30].template filterIt(s: untyped; pred: untyped): untypedReturns the elements for which
pred(referencing the injectedit) holds. Example:filterIt(@[1, 2, 3, 4], it mod 2 == 0) == @[2, 4].template keepItIf(s: untyped; pred: untyped)In-place
filterIt: keeps only the elements of the seqsfor whichpred(referencing the injectedit) holds. Example:keepItIf(data, it mod 2 == 1).template applyIt(varSeq: untyped; op: untyped)In-place
mapIt: replaces each element of the seqvarSeqwithop(referencing the injectedit).opmust yield the element type. Example:applyIt(nums, it * 3).template newSeqWith(len: int64; init: untyped): untypedCreates a
seqof lengthlen, evaluatinginitfor each element — handy for 2D seqs (newSeqWith(5, newSeq[int](3))) or per-element initialization.