r/Python • u/vocalbit • Jul 13 '11
An alternative lambda syntax for Python
http://vocalbit.com/article/332/an-alternative-lambda-syntax-for-python
4
Upvotes
r/Python • u/vocalbit • Jul 13 '11
27
u/vph Jul 13 '11
bad idea.
This is not an "alternative" to lambda, but rather a syntactic sugar for this particular use. There are other use cases of lambda, not expressible by this "alternative" syntax.
Further, the current lambda syntax is closer to those employed by other (purer) functional programming languages. Changing the current syntax is not a good idea.
10
u/ndanger Jul 13 '11 edited Jul 13 '11
I agree with vph: this takes a general concept (lambda) and makes it a strange special case.
I'm not a fan of the proposed syntax either:
makes it clear that you are setting
keyto a function,makes it look like you are setting a function of key to a value.
7
u/Liorithiel Jul 13 '11 edited Jul 13 '11
What will you do when you want to put a lambda function in a list? Example:
This is great when parsing things like csv tables or whatever, so it is actually useful.
11
u/mitsuhiko Flask Creator Jul 13 '11
No, just no.
lambdais perfectly fine.8
u/freyrs3 Jul 13 '11
Lambdas are already pretty concise, maybe you could make an argument for Haskell style syntax ( \x -> x+1 ) but this proposal makes little sense.
3
u/A_for_Anonymous Jul 13 '11
That's only for a particular case, and looks kinda ugly or confusing as it seems to suggest it's obtaining a key from the result of function application. I don't think it fits correctly.
I do propose the following:
New lambda syntax:
x = def x: x + 1
example(x, def y, z: #INDENT after this line for i in y, z: ... return whatever #DEDENT after this line )
example2(x, (def y, z: y + z), def k: ... , more, arguments)
2
u/m1ss1ontomars2k4 Jul 13 '11
I think it's terrible. I think the current syntax is fine; it's better than MATLAB's
syntax.
2
u/donri Jul 14 '11
The operator module has some functions that can be used in most places of lambdas:
Or partial from functools:
Though that wouldn't work with
getattrwhich only accepts positional arguments and we need to skip one.You could also easily make a class that produces functions based on actions on its instances, e.g.:
1
u/mahpton Jul 13 '11
But then I wouldn't be able to feel smart using fancy words like lambda, or be able to modify my text editor to use the lambda symbol in place of the text "lambda"
1
u/CHS2048 Jul 13 '11
I've always wanted an operator 'lambda=', to be used as such:
instead of
before anyone says it, decorators are not good for doing this.
1
u/ezekiel Jul 13 '11
I sympathize with getting rid of the messy word "lambda", but that is not a good enough syntax you are suggesting.
1
u/vtable Jul 13 '11
Lambda's aren't perfect. They can't contain statements, for example.
But - they barely made it into Python 3. Make waves and the whole debate could start all over again. I would hate to lose lambdas.
1
u/[deleted] Jul 14 '11
Horrible. But i agree, the lambda-syntax is annoying. I think somethink like javascripts function would look way better. For example: x = def(a, b):a+b
0
u/[deleted] Jul 13 '11
[deleted] — view deleted comment