It seems to do exactly what a simple function would do, less the use of the return syntax and maybe writing parenthesis. That really doesn't save anybody time. The cost of a () and a return is insignificant. writing asdf=lambda x: is more work than typing def asdf (x):
Maybe lambda does something I'm missing? I think it goes against Pythons whole "there should only be one way of doing stuff" mentality. It probably should be removed.
edit: and this is for python
[python] Having trouble seeing the need to use lambda, ever.
[python] Having trouble seeing the need to use lambda, ever.
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-★ ·:*¨༺꧁༺ ༻꧂༻¨*:·.★-⎽__⎽-⎻⎺⎺⎻-⎽__⎽--⎻⎺⎺⎻-
❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉❉⊱•═•⊰❉⊱•═•⊰❉⊱•═•⊰❉
Re: Having trouble seeing the need to use lambda, ever.
I've never used lambda. Maybe there is a practical performance reason for using it?
Re: Having trouble seeing the need to use lambda, ever.
They're usually used for trivial one-liners, like if you have a list of dicts that you want sorted by their 'id' key,
Not everyone likes them, but I think writing a full-blown function to get the 'id' of a dict would be too verbose.
Code: Select all
list.sort(key=lambda x: x['id'])
Re: [python] Having trouble seeing the need to use lambda, e
I'm not sure how much it differs from Python, but in languages like C++ they can come in pretty handy. It allows you to write callback functions pretty much in-line, sharing much of the context in which they're defined, as opposed to setting up a full function declaration and passing a bunch of variables as parameters, all for something you're only going to use once.
I've also commonly seen them used for generic algorithms; you can have a standard implementation of quicksort, binary search, or what have you, and use it on pretty much anything just by supplying a lambda that defines how to compare two items of an arbitrary type. Again, passing around function pointers would do the same thing, but if it's a one-off, they're a little more heavy-weight than you really need.
I've also commonly seen them used for generic algorithms; you can have a standard implementation of quicksort, binary search, or what have you, and use it on pretty much anything just by supplying a lambda that defines how to compare two items of an arbitrary type. Again, passing around function pointers would do the same thing, but if it's a one-off, they're a little more heavy-weight than you really need.