Advertisement
Guest User

Untitled

a guest
May 27th, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. var Future = require ('data.future'),
  2. R = require ('ramda'),
  3. log = console.log,
  4.  
  5. id = function (x) { return x; },
  6.  
  7. trampoline = function (fn) {
  8. return function () {
  9. var args = arguments;
  10. var f = function () { return fn.apply (this, args); }
  11. while (f && typeof f === 'function') { f = f (); };
  12. return f;
  13. };
  14. },
  15.  
  16. sequenceF = function (M, fxs) {
  17. return R.reduce (_perform, M.of ([]), fxs);
  18.  
  19. function _perform (acc, f) {
  20. var ys,
  21. setYS = function (v) { ys = v; },
  22. appYS = function (x) { ys = R.append (x, ys); }
  23.  
  24. acc.fork (appYS, setYS)
  25. f.fork (appYS, appYS);
  26. return M.of (ys);
  27. };
  28. },
  29.  
  30. sequence = R.curry (function (M, fxs) {
  31. return R.reduce (_perform, M.of([]), fxs);
  32.  
  33. function _perform (acc, f) {
  34. return acc.chain (function (xs) {
  35. return f.chain (function (x) {
  36. return M.of (R.append (x, xs));
  37. });
  38. });
  39. };
  40. }),
  41.  
  42. nil = null;
  43.  
  44. (function main () {
  45. var i = 0, fxs = [];
  46.  
  47. for (i = 0; i < 20000 ; i++) {
  48. fxs = R.append (Future.of (i), fxs);
  49. }
  50.  
  51. sequenceF (Future, fxs).fork (log, log);
  52. // sequence (Future, fxs).fork (log, log);
  53. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement