Skip to content

Instantly share code, notes, and snippets.

@benmerckx
Created October 2, 2019 18:33
Show Gist options
  • Save benmerckx/7d7216476599903096a655d71124c065 to your computer and use it in GitHub Desktop.
Save benmerckx/7d7216476599903096a655d71124c065 to your computer and use it in GitHub Desktop.
class IteratorStream<Item, Quality> extends StreamBase<Item, Quality> {
var iterator: Iterator<Item>;
public function new(iterator)
this.iterator = iterator;
override function next():Future<Step<Item, Quality>>
return switch iterator.hasNext() {
case true: Link(iterator.next(), this);
case false: End;
}
override public function forEach<Safety>(handler:Handler<Item, Safety>) {
return Future.async(function step(cb:Conclusion<Item, Safety, Quality>->Void)
switch iterator.hasNext() {
case true:
handler.apply(iterator.next()).handle(function (s) {
switch s {
case BackOff:
cb(Halted(this));
case Finish:
cb(Halted(this));
case Resume:
step(cb);
case Clog(e):
cb(Clogged(e, this));
}
});
case false: cb(Depleted);
}, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment