Skip to content

Instantly share code, notes, and snippets.

@yshui
Created May 9, 2017 17:20
Show Gist options
  • Select an option

  • Save yshui/716cfe987c89997760cabc2c951ca430 to your computer and use it in GitHub Desktop.

Select an option

Save yshui/716cfe987c89997760cabc2c951ca430 to your computer and use it in GitHub Desktop.
Turn struct with opApply into ranages
struct applyRange(T, ElemType) {
import core.thread;
private {
class rangeFiber : Fiber {
ElemType *tmp = null;
T r;
this(T r) {
this.r = r;
super(&work);
}
int dg(ref ElemType x) {
tmp = &x;
f.yield();
return 0;
}
void work() {
r.opApply(&dg);
tmp = null;
}
}
rangeFiber f;
}
this(T r) {
f = new rangeFiber(r);
f.call();
}
ref ElemType front() {
return *f.tmp;
}
void popFront() {
f.call();
}
bool empty() {
return f.tmp is null;
}
}
struct Iter {
int opApply(int delegate(ref int x) dg) {
foreach(i; 0..10)
dg(i);
return 0;
}
}
void main() {
import std.stdio;
Iter i;
auto x = applyRange!(Iter, int)(i);
foreach(y; x)
writeln(y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment