Skip to content

Instantly share code, notes, and snippets.

@therealklanni
Last active October 2, 2016 22:37
Show Gist options
  • Save therealklanni/ae84f6a94176f02b356f to your computer and use it in GitHub Desktop.
Save therealklanni/ae84f6a94176f02b356f to your computer and use it in GitHub Desktop.
Memoized Y-Combinator in ES6
let Ym = (f, s = JSON.stringify) => (g => g(g))(x => f((...v) => (k => k in x ? x[k] : x[k] = x(x)(...v))(s(v))))
let fib = Ym(f => n => n <= 1 ? n : f(n - 1) + f(n - 2))
console.log('fib(1000)', fib(1000)) // => 4.346655768693743e+208
@therealklanni
Copy link
Author

Yeah, that's what I had found as well. I like putting JSON.stringify in as a default param, so we can use the abbreviated arrow function syntax. Thanks for your feedback!

@therealklanni
Copy link
Author

Updated the gist. I also removed the cache param. The cache will be stored in the anonymous function x instead.

@mozjag
Copy link

mozjag commented Jun 19, 2015

Very clever, using the recursion function as the cache.

I'm using key in cache (now k in x) so you can store false-y values in your cache without having to calculate them over and over.

It's also possible to get rid of some extraneous parentheses, leading to:

let Ym = (f, s = JSON.stringify) => (g => g(g))(x => f((...v) => (k => k in x ? x[k] : x[k] = x(x)(...v))(s(v))));

@therealklanni
Copy link
Author

Oh, I see. I wasn't sure why you used in instead of just trying to read the cached value. Good call, I'll update the code. By the way, post your Twitter handle, so I can credit you for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment