Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda, calling itself into the lambda definition

I'm doing a complicated hack in Python, it's a problem when you mix for+lambda+*args (don't do this at home kids), the boring details can be omited, the unique solution I found to resolve the problem is to pass the lambda object into the self lambda in this way:

for ...
    lambda x=x, *y: foo(x, y, <selflambda>)

It's possible?, thanks a lot.

like image 924
Htechno Avatar asked Mar 27 '26 22:03

Htechno


2 Answers

You are looking for a fixed-point combinator, like the Z combinator, for which Wikipedia gives this Python implementation:

Z = lambda f: (lambda x: f(lambda *args: x(x)(*args)))(lambda x: f(lambda *args: x(x)(*args)))

Z takes one argument, a function describing the function you want, and builds and returns that function.

The function you're looking to build is:

Z(lambda f: lambda x=x, *y: foo(x, y, f))
like image 105
Jason Orendorff Avatar answered Mar 31 '26 03:03

Jason Orendorff


While your question is genuinely weird, try something like:

>>> import functools
>>> f = lambda selflambda, x=x, *y: foo(x, y, selflambda)
>>> f = functools.partial(f, f)
like image 31
Antoine P. Avatar answered Mar 31 '26 03:03

Antoine P.