Recursive iterator to unpack arbitrarily nested sequence

2021-06-20 Thread jyapayne
Related to this discussion, I have created this RFC:

Recursive iterator to unpack arbitrarily nested sequence

2021-06-20 Thread cblake
Good solution, @shirleyquirk. To others/more generally, all this has been discussed before several times and shows up on Forum searches. Also, [this](https://forum.nim-lang.org/t/7020#44192) is a more general `toItr`. The manual is understandably tilted toward pedagogy not generality. While I

Recursive iterator to unpack arbitrarily nested sequence

2021-06-20 Thread shirleyquirk
Ok, my turn. It's not possible to create an arbitrarily-nested seq[seq... at runtime, so first here's a thrown-together nested type type Nested[T] = object case isBase:bool of true: val: T of false: vals: seq[Nested[T]] proc `$`[

Recursive iterator to unpack arbitrarily nested sequence

2021-06-20 Thread demotomohiro
Nice! I didn't know overloaded iterator can be used recursively. When a depth of loop depends on runtime value, compiler cannot know how many loops need to be nested and cannot generate loops. But Nim can expand overloaded recursive inline iterator because a depth of loop can be determined at c

Recursive iterator to unpack arbitrarily nested sequence

2021-06-19 Thread jyapayne
Sooo, modifying @shirleyquirk's solution, this seems to work: iterator flatten[T](source: openArray[T]): auto = ## Flattens an arbitrarily nested ## sequence when T isnot seq: for element in source: yield element else: for each in sour

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread demotomohiro
You cannot write recursive iterator, but you can write recursive template. template flattenTmpl(sq: seq or array, body: untyped): untyped = when sq[0] is (seq or array): for i in sq: flattenTmpl(i, body) else: for i {.inject.} in sq: bod

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread doofenstein
I'm not sure if this is what you're looking for, but I came up with this: import macros macro genIteratorBody(typ: typedesc, source: typed): untyped = var seqType = getType(typ)[1] curIterItem = source curIterBody = newStmtList()

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread HJarausch
Perhaps (see my versions at the end) can be useful for you. I have used it for highly recursive iterators.

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread nocturn9x
As much as I appreciate your feedback, I don't feel comfortable adding 100 lines of code to my project just to make a recursive iterator work. I'll try a recursive procedure, although missing support for recursive iterators is a bit saddening

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread nocturn9x
Any ideas on how to at least do it without iterators? maybe a `flatten` procedure instead?

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread shirleyquirk
Well, if I reduce your example to: iterator flatten[T](source: seq[T]): auto = ## Flattens an arbitrarily nested ## sequence when not (T is seq): for element in source: yield element else: for e in flatten(source

Recursive iterator to unpack arbitrarily nested sequence

2021-06-17 Thread nocturn9x
Is there a way to define recursive iterators? I'd need to unpack an arbitrarily nested sequence of objects and I thought using an iterator would be the most efficient solution. I tried this, but I get is `expression 'flatten' cannot be called` at `flatten(T, source[0])` iterator fl