Skip to content

Instantly share code, notes, and snippets.

@luikore
Created April 4, 2012 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luikore/2300067 to your computer and use it in GitHub Desktop.
Save luikore/2300067 to your computer and use it in GitHub Desktop.
y combinator in ruby
# this is a bit shorter than the one I made several years ago
def Y f
g = -> h, *x { f[h[h], *x] }.curry 2
g[g]
end
factorial = Y -> f, n { n == 0 ? 1 : f[n - 1] * n }
puts factorial.(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment