iterator `/`[T](sequence: seq[T],
filter: proc(e:T):bool {.closure.}) : T =
for element in sequence:
if (filter(element)):
yield element
iterator `>>`[I,O](sequence: seq[I],
map: proc(e:I):O {.closure.}) : O =
for element in sequence:
yield map(element)
iterator `/>>`[I,O](sequence: seq[I],
filtermap:tuple[
f:proc(e:I):bool {.closure.},
m:proc(e:I):O {.closure.}]) : O =
for element in sequence:
if (filtermap.f(element)):
yield filtermap.m(element)
proc isEven(x:int): bool = result =
(x and 1) == 0
proc square(x:int): int = result =
x * x
let list = @[1,2,3,4,5,6,7,8,9]
echo ("--- evens")
for item in list / isEven : echo(item)
echo ("--- squares")
for item in list >> square : echo(item)
echo ("--- squares of evens, only")
# this doesn't compile. Generic types are not inferred
#for item in list />> (isEven, square) : echo(item)
As you can see, it works ok, but when I uncomment last line (/>> usage), I get a compiler error:
What am I doing wrong? Also, I don't understand why {.closure.} is required for avoiding a compiler warning.
And by the way, are iterators first class (as procedures are)? Can I return an iterator from a function and use iterators for arguments' types.
Greetings.
I'm testing from github "latest" version (I updated my repo last friday), so it's 0.9.
I saw the flag, but I was resiliant to use it (I rather fixing this kind of things explicitly in code, better than from command line). You know, two weeks from know and you're looking the warning again, with no clues why it didn't appear before... ;)
I'll have a look at sequtils. By now, I'm trying to familiarize to language (trying to guess how I could change this iterator code by nice looking term rewriting templates) and have not seen at libraries, yet. I assumed some functional tools would exist for sequences, but wanted to try implementing my own. As I said, a quite enjoyable language :)
Better than Clojure? Well, maybe... at this point, I feel you surpassed C++ in many points, so you're getting nearer.
Thanks for all your support and your work on Nimrod. I believe this is a nice language to work on, so I'll try to read compiler's implementation, just in case it could be of some help for fixing one bug or two in the future.
Greetings.