Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // `fix` is an Y-combinator implementation (recursive calls in lambda calculus):
- // Y = \f -> (\x -> f (x x))(\x -> f (x x))
- // fix :: (a -> a) -> a
- const fix = f => (x => f(y => (x(x))(y)))(x => f(y => (x(x))(y)));
- // Generator function. Inner signature should correspond to actual function interface.
- // mapgen :: ((a -> b) -> [a] -> [b]) -> (a -> b) -> [a] -> [b]
- const mapgen = map => f => list => list.length === 0 ? [] : [f(list[0])].concat(map(f)(list.slice(1)));
- // Memoization of a function of 1 argument. For functions of more arguments explicit work with `arguments` needed.
- // memoize :: (a -> b) -> a -> b
- const memoize = fn => x => {
- const hash = x === Object(x) ? JSON.stringify(x) : x.toString();
- fn.memoize || (fn.memoize = {});
- return (hash in fn.memoize) ? fn.memoize[hash] : (fn.memoize[hash] = fn(x));
- };
- // map :: (a -> b) -> [a] -> [b]
- const map = fix(memoize(mapgen));
Add Comment
Please, Sign In to add comment