-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.d
39 lines (34 loc) · 886 Bytes
/
generator.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module nemoutils.generator;
class RangeGenerator (alias T, ElementType) {
ElementType front;
import core.thread;
auto yield (ElementType returnType) {
this.front = returnType;
fiber.yield;
}
this () {
fiber = new Fiber (() { T (&yield); });
fiber.call;
}
auto popFront () {
fiber.call;
}
auto empty () {
return fiber.state == Fiber.State.TERM;
}
Fiber fiber = null;
}
auto genRange (ElementType, string functionBody) () {
mixin (`return new RangeGenerator!((void delegate (`~ ElementType.stringof ~`) yield) { ` ~ functionBody ~ `}, ` ~ ElementType.stringof ~ `);`);
}
unittest {
import std.range;
assert (genRange! (int, q{
int i = 0;
while (true) {
yield (i);
i++;
}
})
.take (4).array == [0, 1, 2, 3]);
}