Created
June 14, 2019 07:24
-
-
Save clinuxrulz/1bd47cda980cc256bd012506af268e54 to your computer and use it in GitHub Desktop.
Coalesce free Cell::apply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Cell<A> { | |
. | |
. | |
. | |
/** | |
* Apply a value inside a cell to a function inside a cell. This is the | |
* primitive for all function lifting. | |
*/ | |
static apply<A,B>(cf : Cell<(a : A) => B>, ca : Cell<A>, sources? : Source[]) : Cell<B> { | |
return Transaction.run(() => { | |
let pumping = false; | |
const state = new ApplyState<A,B>(), | |
out = new StreamWithSend<B>(), | |
cf_updates = Operational.updates(cf), | |
ca_updates = Operational.updates(ca), | |
pump = () => { | |
if (pumping) { | |
return; | |
} | |
pumping = true; | |
Transaction.currentTransaction.prioritized(out.getVertex__(), () => { | |
let f = state.f_present ? state.f : cf.sampleNoTrans__(); | |
let a = state.a_present ? state.a : ca.sampleNoTrans__(); | |
out.send_(f(a)); | |
pumping = false; | |
}); | |
}, | |
src1 = new Source( | |
cf_updates.getVertex__(), | |
() => { | |
return cf_updates.listen_(out.getVertex__(), (f : (a : A) => B) => { | |
state.f = f; | |
state.f_present = true; | |
pump(); | |
}, false); | |
} | |
), | |
src2 = new Source( | |
ca_updates.getVertex__(), | |
() => { | |
return ca_updates.listen_(out.getVertex__(), (a : A) => { | |
state.a = a; | |
state.a_present = true; | |
pump(); | |
}, false); | |
} | |
); | |
out.setVertex__(new Vertex("apply", 0, | |
[src1, src2].concat(sources ? sources : []) | |
)); | |
return out.holdLazy(new Lazy<B>(() => | |
cf.sampleNoTrans__()(ca.sampleNoTrans__()) | |
)); | |
}); | |
} | |
. | |
. | |
. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment