Questions tagged [lambda]

DO NOT USE FOR THE AWS SERVICE (use [aws-lambda] for those questions!) Lambdas are anonymous functions or closures in programming languages such as Lisp, C#, C++, Lua, Python, Ruby, JavaScript, Java, Excel or Google sheets. (Also, lambda expression.)

This term originated with the lambda calculus, a Turing-complete model of computation which uses only functions, called lambda expressions. They are of the form λ<argument name(s)>.<expression>; the point is that occurrences of the argument <argument name(s)> inside the expression <expression> are substituted with the values of the arguments. An example is λx.x, the identity function.

In programming languages such as , , , and , lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus. An anonymous function enables definition of a function without binding to an identifier. Lambda expressions are supported in since version 8, in since version 11.

Android does not currently use Java 8, but Android Studio and other IDEs (IntelliJ Idea, etc.) automagically collapses "Closures" (anonymous classes implementing one method) into lambda expressions.

References

29774 questions
126
votes
2 answers

Is it Linq or Lambda?

I know that this is Linq: var _Results = from item in _List where item.Value == 1 select item; And I know this is Lambda: var _Results = _List.Where(x => x.Value == 1); Editor's note: the above is not merely…
Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
126
votes
2 answers

Lambda capture and parameter with same name - who shadows the other? (clang vs gcc)

auto foo = "You're using g++!"; auto compiler_detector = [foo](auto foo) { std::puts(foo); }; compiler_detector("You're using clang++!"); clang++ 3.6.0 and newer print out "You're using clang++!" and warn about the capture foo being unused. g++…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
126
votes
6 answers

_=> what does this underscore mean in Lambda expressions?

What does an lambda expression like _=> expr mean? What is the purpose of _ as input to lambda? Example: int count = 0; list.ForEach(_ => count += 1);
Prasad
  • 1,347
  • 2
  • 9
  • 7
126
votes
5 answers

Return from lambda forEach() in java

I am trying to change some for-each loops to lambda forEach()-methods to discover the possibilities of lambda expressions. The following seems to be possible: ArrayList playersOfTeam = new ArrayList(); for (Player player :…
samu
  • 1,936
  • 4
  • 22
  • 26
125
votes
9 answers

C++ lambda with captures as a function pointer

I was playing with C++ lambdas and their implicit conversion to function pointers. My starting example was using them as callback for the ftw function. This works as expected. #include #include using namespace std; int main() { …
duncan
  • 6,113
  • 3
  • 29
  • 24
125
votes
6 answers

Python's lambda with underscore for an argument?

What does the following code do? a = lambda _:True From what I read and tested in the interactive prompt, it seems to be a function that returns always True. Am I understanding this correctly? I hope to understand why an underscore (_) was used as…
laycat
  • 5,381
  • 7
  • 31
  • 46
125
votes
5 answers

Why do some C# lambda expressions compile to static methods?

As you can see in the code below, I have declared an Action<> object as a variable. Would anybody please let me know why this action method delegate behaves like a static method? Why does it return true in the following code? Code: public static…
nunu
  • 3,184
  • 10
  • 43
  • 58
124
votes
6 answers

A suitable 'do nothing' lambda expression in python?

I sometimes find myself wanting to make placeholder 'do nothing' lambda expressions, similar to saying: def do_nothing(*args): pass But the following syntax is illegal since lambda expressions attempt to return whatever is after the colon, and…
user3002473
  • 4,835
  • 8
  • 35
  • 61
123
votes
11 answers

Do lambda expressions have any use other than saving lines of code?

Do lambda expressions have any use other than saving lines of code? Are there any special features provided by lambdas which solved problems which weren't easy to solve? The typical usage I've seen is that instead of writing…
Vikash
  • 2,046
  • 3
  • 23
  • 30
123
votes
2 answers

Cell-var-from-loop warning from Pylint

For the following code: for sort_key, order in query_data['sort']: results.sort(key=lambda k: get_from_dot_path(k, sort_key), reverse=(order == -1)) Pylint reported an error: Cell variable sort_key defined in loop…
xis
  • 24,330
  • 9
  • 43
  • 59
123
votes
9 answers

Java8 Lambdas vs Anonymous classes

Since Java8 has been recently released and its brand new lambda expressions looks to be really cool, I was wondering if this means the demise of the Anonymous classes that we were so used to. I've been researching a bit about this and found some…
Amin Abu-Taleb
  • 4,423
  • 6
  • 33
  • 50
122
votes
1 answer

Why do arrow functions not have the arguments array?

function foo(x) { console.log(arguments) } //foo(1) prints [1] but var bar = x => console.log(arguments) gives the following error when invoked in the same way: Uncaught ReferenceError: arguments is not defined
Conqueror
  • 4,265
  • 7
  • 35
  • 41
122
votes
5 answers

Lambda Expression and generic defined only in method

Suppose I've a generic interface: interface MyComparable> { public int compare(T obj1, T obj2); } And a method sort: public static > void sort(List list, MyComparable comp) { //…
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
122
votes
6 answers

How is std::function implemented?

According to the sources I have found, a lambda expression is essentially implemented by the compiler creating a class with overloaded function call operator and the referenced variables as members. This suggests that the size of lambda expressions…
user2545918
121
votes
6 answers

What does lambda with 2 arrows mean in Java 8?

I have read several Java 8 tutorials before. Right now I encountered following topic: Does java support Currying? Here, I see following code: IntFunction curriedAdd = a -> b -> a +…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710