Guest User

Untitled

a guest
Feb 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. export function cellTrace<A>(ca: sodium.Cell<A>, extractor: (a: A) => (sodium.Stream<any>|sodium.Cell<any>)[]): sodium.Cell<A> {
  2. let cKeepAlive = sodium.Cell.switchC(ca.map(
  3. a =>
  4. cellLiftArray(
  5. extractor(a).map(
  6. x => {
  7. if (x instanceof sodium.Stream) {
  8. return x.hold({} as any);
  9. } else {
  10. return x;
  11. }
  12. }
  13. )
  14. )
  15. ));
  16. return ca.map(sodium.lambda1(a => a, [cKeepAlive]));
  17. }
  18.  
  19. function cellLiftArray<A>(ca: sodium.Cell<A>[]): sodium.Cell<A[]> {
  20. return _cellLiftArray(ca, 0, ca.length);
  21. }
  22.  
  23. function _cellLiftArray<A>(ca: sodium.Cell<A>[], fromIdx: number, toIdx: number): sodium.Cell<A[]> {
  24. if (toIdx - fromIdx == 0) {
  25. return new sodium.Cell<A[]>([]);
  26. } else if (toIdx - fromIdx == 1) {
  27. return ca[fromIdx].map(a => [a]);
  28. } else {
  29. let pivot = Math.floor((fromIdx + toIdx) / 2);
  30. return _cellLiftArray(ca, fromIdx, pivot).lift(
  31. _cellLiftArray(ca, pivot, toIdx),
  32. (array1, array2) => array1.concat(array2)
  33. );
  34. }
  35. }
Add Comment
Please, Sign In to add comment