Lambda instead of “if” statement
-
21-12-2020 - |
Question
I've heard that it is possible to substitute an if
statement by using a lambda.
Is this possible in Python? If so, how?
Solution
Like the others, I'm not sure what you're asking about, but I'm willing to have a guess.
I sometimes use lambdas in a bit of a hacky way processing results from API calls.
Say for example an element of an API call result should be a numeric string which I'd want as an integer, but occasionally it returns something else.
You could defined a lambda to turn a string into an integer if it is comprised of digits:
lambda x: x and x.isdigit() and int(x) or None
This is avoiding an if
statement, but not because of the lambda
, you could do the same as a function:
def f(x):
return x and x.isdigit() and int(x) or None
Update
Less buggy hack, courtesy of Paul McGuire:
lambda x: int(x) if x and x.isdigit() else None
i.e. as int('0')
returns an equivalent of False
the lambda might surprise you by returning None
when you wanted 0
OTHER TIPS
Perhaps you are referring to something like this (Lambda calculus)?
If = lambda test, x, y: test(x, y)
True = lambda x, y: x
False = lambda x, y: y
Which you could use like...
# I guess you have to convert them sometimes... oh well
C = lambda b: [False, True][b]
x = If(C(2 > 3), "Greater", "Less")
print(x)
# "Less"
But now things start to fall apart...
If(C(2 > 3), print("Greater"), print("Less"))
# Invalid syntax unless you use
# from __future__ import print_function
# And if you do, it prints both!
# (Because python has eager evaluation)
# So we could do
True = lambda x, y: x()
False = lambda x, y: y()
# And then
If(C(2 > 3), lambda:print("Greater"), lambda:print("Less"))
# "Less"
So, not so pretty, or useful. But it works.
I might be seriously off, but I'd imagine this means something like:
filter(lambda x: x > 0, list_of_values)
Would return you the elements from list_of_values
which have a value greater than 0.
Following is a little trick inspired by Smalltalk language core, which does not use if statement nor ternary operator, yet working as a conditional execution mechanism. :-)
#!/usr/bin/env python
class ATrue:
def ifThen(self,iftrue): iftrue()
def ifThenElse(self,iftrue,iffalse): return iftrue()
def andThen(self,other): return other()
def orElse(self,other): return self
class AFalse:
def ifThen(self,iftrue): pass
def ifThenElse(self,iftrue,iffalse): return iffalse()
def andThen(self,other): return self
def orElse(self,other): return other()
def echo(x): print x
if __name__=='__main__':
T = ATrue()
F = AFalse()
x = T # True
y = T.andThen(lambda: F) # True and False
z = T.orElse(lambda: F) # True or False
x.ifThenElse( lambda: echo("x is True"), lambda: echo("x if False"))
y.ifThenElse( lambda: echo("y is True"), lambda: echo("y if False"))
z.ifThenElse( lambda: echo("z is True"), lambda: echo("z if False"))
UPDATE: Tidy up some symbols to avoid confusion and make the point clear. And added code to show how short-cut evaluation of logical operators can be implemented.
if condition:
do_stuff()
else:
dont()
is
(lambda x: do_stuff() if x else dont())(condition)
But it's not clear what you're looking for.
Note that this is not an if
statement -- it's a ternary operation. In Python, they just both use the word if
. See for example Lambda "if" statement? for this in C#.