Skip to content

Instantly share code, notes, and snippets.

@clinuxrulz
Last active February 20, 2019 02:51
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/6298db3e318db46f6c3e06f5c5eafeeb to your computer and use it in GitHub Desktop.
Save clinuxrulz/6298db3e318db46f6c3e06f5c5eafeeb to your computer and use it in GitHub Desktop.
Tracking high order dependencies
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