Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export function cellTrace<A>(ca: sodium.Cell<A>, extractor: (a: A) => (sodium.Stream<any>|sodium.Cell<any>)[]): sodium.Cell<A> {
- let cKeepAlive = sodium.Cell.switchC(ca.map(
- a =>
- cellLiftArray(
- extractor(a).map(
- x => {
- if (x instanceof sodium.Stream) {
- return x.hold({} as any);
- } else {
- return x;
- }
- }
- )
- )
- ));
- return ca.map(sodium.lambda1(a => a, [cKeepAlive]));
- }
- function cellLiftArray<A>(ca: sodium.Cell<A>[]): sodium.Cell<A[]> {
- return _cellLiftArray(ca, 0, ca.length);
- }
- function _cellLiftArray<A>(ca: sodium.Cell<A>[], fromIdx: number, toIdx: number): sodium.Cell<A[]> {
- if (toIdx - fromIdx == 0) {
- return new sodium.Cell<A[]>([]);
- } else if (toIdx - fromIdx == 1) {
- return ca[fromIdx].map(a => [a]);
- } else {
- let pivot = Math.floor((fromIdx + toIdx) / 2);
- return _cellLiftArray(ca, fromIdx, pivot).lift(
- _cellLiftArray(ca, pivot, toIdx),
- (array1, array2) => array1.concat(array2)
- );
- }
- }
Add Comment
Please, Sign In to add comment