Sequences and queries
Seq vs gen and querying patterns
Nanyx has two enumerable abstractions: seq(a) and gen(a).
- A
seqis known to be finite and safe to iterate. - A
gen(generator) can be infinite, lazy, or non-deterministic.
Iteration safety
for 0.. | i -> println("Hello") -- Error: cannot iterate over a gen
for 1..10 | i -> println("Hello") -- OK
for 1.. \take(10) | i -> println("Hello") -- OK: take turns a gen into a seq
Enumerating with indexes
A gen may be zipped with a seq in a for loop, because the finite side bounds the iteration:
def items: seq(string) = ...
for items, 0.. | item, i -> println("Item {i} is {item}")
Iterators
Both seq and gen provide getIterator. The main difference is that a gen iterator can be impure, while a seq iterator is pure, reflecting the finiteness guarantee.