Guest User

Untitled

a guest
Jun 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. const lazy = (f, ...args) => () => {
  2. const values = args.map(arg => (arg instanceof Function) ? arg() : arg);
  3. return f(...values);
  4. }
  5.  
  6. const emptyStream = () => null;
  7. const stream = (h, ...tail) => [h, lazy(...tail)];
  8.  
  9. const head = ([h]) => h;
  10. const tail = ([h, t]) => t();
  11.  
  12. const map = f => s => stream(f(head(s)), map(f), lazy(tail, s));
  13.  
  14. const nums = n => stream(n, nums, n + 1);
  15.  
  16. const listToStream = ([head, ...tail]) => stream(head, tail.length > 0 ? listToStream(tail) : emptyStream);
  17.  
  18. const take = n => s => {
  19. let array = [];
  20. while(n > 0) {
  21. array.push(head(s))
  22. s = tail(s);
  23. n--;
  24. }
  25. return array;
  26. }
Add Comment
Please, Sign In to add comment