Skip to content

Instantly share code, notes, and snippets.

@AxGord
Last active September 26, 2015 01:16
Show Gist options
  • Save AxGord/859c6290a7f440475afe to your computer and use it in GitHub Desktop.
Save AxGord/859c6290a7f440475afe to your computer and use it in GitHub Desktop.
Using pony signals for streams
static function streamToString() {
var streamInput = new Event1<Int>();
var streamOutput:Signal1<Int> = streamInput;
var subStream = streamOutput.convert1(
function(e:Event1<String>, v:Int) e.dispatch(v == 2 ? 'two' : Std.string(v))
);
subStream.add(function(s:String) trace(s));
streamInput.dispatch(1);// => 1
streamInput.dispatch(2);// => two
streamInput.dispatch(3);// => 3
}
static function streamCalc() {
var streamInput = new Event2<Int, Int>();
var streamOutput:Signal2<Int, Int> = streamInput;
var subStream = streamOutput.convert1(
function(e:Event1<Int>, a:Int, b:Int) e.dispatch(a+b)
);
subStream.add(function(s:Int) trace(s));
streamInput.dispatch(1, 2);// => 3
streamInput.dispatch(3, 4);// => 7
streamInput.dispatch(5, 6);// => 11
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment