- Can only have one line
- Can only use expressions, not statements. E.g. `print`s, loops, conditionals are out.
- Overall just kinda clunky
Here's an SO post about lambdas where the answer is "Use def instead." https://stackoverflow.com/questions/14843777/how-to-write-py...
In [4]: f = lambda x: "Yes" if x else "No"
In [5]: f(True)
Out[5]: 'Yes'
In [6]: f(False)
Out[6]: 'No'
It's Python's version of ternary operators, so not sure if that counts as a "true" conditional; but it is one.Loops don't work, but list comprehensions do, and they are definitely the way to go here. Multi-line loops deserve a `def`.
print is a function (and thus can be used in expressions)
python has conditional expressions (<true-val> if <cond> else <false-val>)
loops are a limitation, though comprehensions, map(), functools.reduce(), and the itertools module can allow lots of looping functionality in an expression.
println = lambda s: sys.stdout.write(s + '\n')
Not that it really makes things much better, but, at least it shows you can do it.There’s no good way to add support for full anonymous functions to Python’s grammar. One of the rules that makes significant-whitespace work elegantly is that statements can contain expressions, but never vice-versa.
x => x * 2
instead of lambda x : x * 2Perhaps with the move to a PEG parser, this syntax could now be supported?
λx: x*2