Skip to content

Instantly share code, notes, and snippets.

@clinuxrulz
Last active June 16, 2019 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clinuxrulz/a06eba0d727a372b0016d19783f067fc to your computer and use it in GitHub Desktop.
Save clinuxrulz/a06eba0d727a372b0016d19783f067fc to your computer and use it in GitHub Desktop.
class MergeState<A> {
constructor() {}
left : A = null;
left_present : boolean = false;
right : A = null;
right_present : boolean = false;
}
export class Stream<A> {
.
.
.
merge(s : Stream<A>, f : ((left : A, right : A) => A) | Lambda2<A,A,A>) : Stream<A> {
const ff = Lambda2_toFunction(f);
const mergeState = new MergeState<A>();
let pumping = false;
const out = new StreamWithSend<A>(null);
const pump = () => {
if (pumping) {
return;
}
pumping = true;
Transaction.currentTransaction.prioritized(out.getVertex__(), () => {
if (mergeState.left_present && mergeState.right_present) {
out.send_(ff(mergeState.left, mergeState.right));
} else if (mergeState.left_present) {
out.send_(mergeState.left);
} else if (mergeState.right_present) {
out.send_(mergeState.right);
}
mergeState.left = null;
mergeState.left_present = false;
mergeState.right = null;
mergeState.right_present = false;
pumping = false;
});
};
const vertex = new Vertex("merge", 0,
[
new Source(
this.vertex,
() => this.listen_(out.vertex, (a : A) => {
mergeState.left = a;
mergeState.left_present = true;
pump();
}, false)
),
new Source(
s.vertex,
() => s.listen_(out.vertex, (a : A) => {
mergeState.right = a;
mergeState.right_present = true;
pump();
}, false)
)
].concat(toSources(Lambda2_deps(f)))
);
out.vertex = vertex;
return out;
}
.
.
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment