Is there a simple method of getting a closure iterator from a sequence? I'd
like to pass one around like so:
proc show_next(it: iterator(): string): iterator(): string =
echo it()
it
"hello world".split().show_next().show_next()
Run
Obviously the above doesn't compile, but should get the point across as to what
I'm going for.
I see there's an items function, but it returns an {.inline.} iterator, so that
won't work. I also see that in sequtils is another items function, but I can't
seem to get anything using it to compile (below showing output from the
playground):
/usercode/in.nim(8, 15) Error: attempting to call routine: 'items'
Run
I can roll my own:
proc seq_to_iter[T](xs: seq[T]): iterator(): T =
return iterator(): T =
for x in xs:
yield x
Run
But was generally hoping that there was something already built-in that could
be easily used.
P.S. In the above seq_to_iter, why is return required? Without it the compiler
complains with: identifier expected, but got '('. I find that there are some
esoteric rules that govern when return needs to be present for the parser and
when it doesn't. Are these numerated anywhere?