Last active
February 20, 2019 02:51
-
-
Save clinuxrulz/6298db3e318db46f6c3e06f5c5eafeeb to your computer and use it in GitHub Desktop.
Tracking high order dependencies
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 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).map(sodium.lambda1(x => x, ca)); | |
} | |
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) | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment