Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const lazy = (f, ...args) => () => {
- const values = args.map(arg => (arg instanceof Function) ? arg() : arg);
- return f(...values);
- }
- const emptyStream = () => null;
- const stream = (h, ...tail) => [h, lazy(...tail)];
- const head = ([h]) => h;
- const tail = ([h, t]) => t();
- const map = f => s => stream(f(head(s)), map(f), lazy(tail, s));
- const nums = n => stream(n, nums, n + 1);
- const listToStream = ([head, ...tail]) => stream(head, tail.length > 0 ? listToStream(tail) : emptyStream);
- const take = n => s => {
- let array = [];
- while(n > 0) {
- array.push(head(s))
- s = tail(s);
- n--;
- }
- return array;
- }
Add Comment
Please, Sign In to add comment