nimjautils

    Dark Mode
Search:
Group by:

Types

Loop[T] = object
  index*: int                ## which element (start from 1)
  index0*: int               ## which elemen (start from 0)
  first*: bool               ## if this is the first loop iteration
  last*: bool                ## if this is the last loop iteration
  previtem*: Option[T]       ## get the item from the last loop iteration
  nextitem*: Option[T]       ## get the item from the next loop iteration
  length*: int               ## the length of the seq, (same as mySeq.len())
  revindex0*: int            ## which element, counted from the end (last one is 0)
  revindex*: int             ## which element, counted from the end (last one is 1)
  
Loopable[T] = concept x {...}{.explain.}
    x.len() is int
    x[int] is T
    x.items is T

Procs

proc cycle[T](loop: Loop; elems: openArray[T]): T
within a loop you can cycle through elements:
{% for loop, row in rows.loop() %}
    <li class="{{ loop.cycle(@["odd", "even"]) }}">{{ row }}</li>
{% endfor %}

Iterators

iterator loop[T](a: Loopable[T]): tuple[loop: Loop[T], val: T] {...}{.inline.}
yields a Loop object with every item. Inside the loop body you have access to the following fields.
{% for loop, row in rows.loop() %}
    {{ loop.index0 }}
    {{ loop.index }}
    {{ loop.revindex0 }}
    {{ loop.revindex }}
    {{ loop.length }}
    {% if loop.first %}The first item{% endif %}
    {% if loop.last %}The last item{% endif %}
    {% if loop.previtem.isSome %}{{ loop.previtem.get() }}{% endif %}
    {% if loop.nextitem.isSome %}{{ loop.nextitem.get() }}{% endif %}
    <li class="{{ loop.cycle(@["odd", "even"]) }}">{{row}}</li>
{% endfor %}

however, the element you iterate over must match the Concept Loopable.