-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
Description
This is extracted from https://github.com/def-/nim-iterutils . Since it compiles for the first time since Nimrod 0.9.4, I'm not sure what exactly broke it. I believe this to be related to the closure changes and tried the suggested changes, but couldn't fix it:
from sequtils import toSeq
type Iterable*[T] = (iterator: T) | Slice[T]
## Everything that can be iterated over, iterators and slices so far.
proc toIter*[T](s: Slice[T]): iterator: T =
## Iterate over a slice.
iterator it: T {.closure.} =
for x in s.a..s.b:
yield x
return it
proc toIter*[T](i: iterator: T): iterator: T =
## Nop
i
iterator map*[T,S](i: Iterable[T], f: proc(x: T): S): S =
let i = toIter(i)
for x in i():
yield f(x)
proc filter*[T](i: Iterable[T], f: proc(x: T): bool): iterator: T =
## Iterates through an iterator and yields every item that fulfills the
## predicate `f`.
##
## .. code-block:: nim
## for x in filter(1..11, proc(x): bool = x mod 2 == 0):
## echo x
let i = toIter(i)
iterator it: T {.closure.} =
for x in i():
if f(x):
yield x
result = it
iterator filter*[T](i: Iterable[T], f: proc(x: T): bool): T =
let i = toIter(i)
for x in i():
if f(x):
yield x
var it = toSeq(filter(2..10, proc(x: int): bool = x mod 2 == 0))
echo it # @[2, 4, 6, 8, 10]
it = toSeq(map(filter(2..10, proc(x: int): bool = x mod 2 == 0), proc(x: int): int = x * 2))
echo it # Expected output: @[4, 8, 12, 16, 20], Actual output: @[]