src/slicerator

Source   Edit  

Iterators

iterator `[]`[T](a: openArray[T]; slice: HSlice[int, BackwardsIndex]): T
Immutable slice iteration over an openarray, taking BackwardsIndex Source   Edit  
iterator `[]`[T](a: openArray[T]; slice: Slice[int]): T
Immutable slice iteration over an openarray Source   Edit  
iterator findAll[T](a: openArray[T]; val: T): int
Iterates the openArray yielding indices that match val Source   Edit  
iterator mFindAll[T](a: var openArray[T]; val: T): var T
Iterates the openarray yielding mutable values that match val Source   Edit  
iterator pairs[T](a: openArray[T]; slice: HSlice[int, BackwardsIndex]): (int, T)
Immutable slice iteration over an openarray, taking BackwardsIndex, yielding index and element Source   Edit  
iterator pairs[T](a: openArray[T]; slice: HSlice[int, int]): (int, T)
Immutable slice iteration over an openarray, yielding index and element Source   Edit  
iterator revItems[T](a: openArray[T]): T
Reversed immutable items over an openArray Source   Edit  
iterator revMitems[T](a: var openArray[T]): var T
Reversed mutable items over an openArray Source   Edit  
iterator rFindAll[T](a: openArray[T]; val: T): int
Iterates the openArray backwards yield all indices that match val Source   Edit  
iterator rMFindAll[T](a: var openArray[T]; val: T): var T
Iterates the openArray backwards yielding all mutable values that match val Source   Edit  
iterator `{}`[T](a: var openArray[T]; slice: HSlice[int, BackwardsIndex]): var T
Mutable slice iteration over an openarray, taking BackwardsIndex Source   Edit  
iterator `{}`[T](a: var openArray[T]; slice: Slice[int]): var T
Mutable slice iteration over an openarray Source   Edit  

Macros

macro filter(forLoop: ForLoopStmt): untyped
Iterator based 'filter', runs the iterator yielding only on those that match the expression. Can be used for x in filter(y, x == 1) or for i, x in filter(y, x == 3).

Example:

let data = [10, 20, 30]
for i, x in filter(data, x == 10):
  assert x == 10
Source   Edit  
macro groups(body: ForLoopStmt): untyped
Allows iterating over a user defined selection of values in an open array

Example:

for x, y in groups [100, 300]:
  assert [x, y] == [100, 300]
for x, y in groups [10, 20, 30, 60, 50, 100, 90, 180]:
  assert x * 2 == y
Source   Edit  
macro map(forLoop: ForLoopStmt): untyped
Iterator based map, iterates over all values yielding the expression applied to the values. Can be used for x in map(y, x + 1) or for i, x in map(y, x + 3).

Example:

let data = [10, 20, 30]
for i, x in map(data, x * 3):
  assert data[i] * 3 == x
Source   Edit  
macro zip(others: varargs[untyped]): untyped
Iterates over the iterators making a seq[tuple] of them all, tuple coresponds to the passed in iterators

Example:

assert zip([10, 20, 30], ['a', 'b', 'c']) == @[(10, 'a'), (20, 'b'), (30, 'c')]
Source   Edit  
macro zipIter(forBody: ForLoopStmt): untyped
A version of zip that captures iterators as closures which can improve speed and reduce memory usage. Supports for (x, y) in zipIter(a.items, b.items) and for x, y in zipIter(a.items, b.items).

Example:

let
  a = [10, 20, 30]
  b = "abcdef"
var count = 0
for (x, y) in zipIter(a.items, b.items):
  echo x, y # should run 3 times
  inc count
assert count == 3
Source   Edit  

Templates

template forMItems[T](a: var openArray[T]; indexName, valName, body: untyped): untyped
Sugar for iterating over mutable entries getting their indices and value Source   Edit  
template iterRange(iter, val: untyped; rng: Slice[int]; body: untyped)
Only runs code for a given range of iterations Source   Edit  
template map[T; Y](i: iterable[T]; p: proc (x: T): Y): untyped
Source   Edit  
template skipIter(iter, val: untyped; toSkip: Natural; body: untyped)
Skip over a certain number of iterations Source   Edit