Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
55 lines (37 sloc) 1.3 KB
Y = lambda f: lambda *args: f(Y(f))(*args)

def Y(f):
    def y(*args):
        y_function = f(Y(f))
        return y_function(*args)
return y

>>> Y = lambda f: lambda *args: f(Y(f))(*args)
>>> def factorial(combinator):
...     def _factorial(n):
...         if n:
...             return n * combinator(n - 1)
...         else:
...             return 1
...     return _factorial
>>> Y(factorial)(5)
120

>>> Y = lambda f: lambda *args: f(Y(f))(*args)
>>> Y(lambda c: lambda n: n and n * c(n - 1) or 1)(5)
120

>>> Y = lambda f: lambda *args: f(Y(f))(*args)
>>> Y(lambda c: lambda n: n * c(n - 1) if n else 1)(5)
120

>>> quicksort = Y(lambda f:
...     lambda x: (
...         f([item for item in x if item < x[0]])
...         + [y for y in x if x[0] == y]
...         + f([item for item in x if item > x[0]])
...     ) if x else [])
>>> quicksort([1, 3, 5, 4, 1, 3, 2])
[1, 1, 2, 3, 3, 4, 5]