Ok, I got it working with low level iterator code:
proc len[T](it : (iterator : T)) : Natural =
for i in it():
result += 1
proc filter[T](it: (iterator : T), f: proc(x: T): bool): (iterator: T) =
return iterator(): T =
while true:
let x = it()
if finished(it):
break
else:
if f(x):
yield x
proc simpleSeqIterator[T](s :seq[T]) : (iterator : T) =
iterator it: T {.closure.} =
for x in s:
yield x
return it
let a = toSeq(1 .. 99)
echo len(a)
echo len(simpleSeqIterator(a))
echo len(filter(simpleSeqIterator(a), proc(x : int) : bool = true))
I also once tried to do functional programming in Nim, but there is a lot of
friction to do so at the moment. One thing I did that could also be useful for
you was a lifting template that allowed me to transform an inline iterator into
a closure iterator, so that I could use all those iterators that were only
defined as inline iterators in the standard library more useful.