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
325
votes
6 answers

E731 do not assign a lambda expression, use a def

I get this pep8 warning whenever I use lambda expressions. Are lambda expressions not recommended? If not why?
Kechit Goyal
  • 3,952
  • 3
  • 20
  • 21
324
votes
12 answers

Why does C++11's lambda require "mutable" keyword for capture-by-value, by default?

Short example: #include int main() { int n; [&](){n = 10;}(); // OK [=]() mutable {n = 20;}(); // OK // [=](){n = 10;}(); // Error: a by-value capture cannot be modified in a non-mutable lambda …
kizzx2
  • 18,775
  • 14
  • 76
  • 83
324
votes
1 answer

A positive lambda: '+[]{}' - What sorcery is this?

In Stack Overflow question Redefining lambdas not allowed in C++11, why?, a small program was given that does not compile: int main() { auto test = []{}; test = []{}; } The question was answered and all seemed fine. Then came Johannes…
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
320
votes
17 answers

C# Lambda expressions: Why should I use them?

I have quickly read over the Microsoft Lambda Expression documentation. This kind of example has helped me to understand better, though: delegate int del(int i); del myDelegate = x => x * x; int j = myDelegate(5); //j = 25 Still, I don't…
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
317
votes
10 answers

Combining two expressions (Expression>)

I have two expressions of type Expression> and I want to take to OR, AND or NOT of these and get a new expression of the same type Expression> expr1; Expression> expr2; ... //how to do this (the code below…
BjartN
  • 5,327
  • 6
  • 28
  • 32
306
votes
12 answers

Using Java 8's Optional with Stream::flatMap

The new Java 8 stream framework and friends make for some very concise Java code, but I have come across a seemingly-simple situation that is tricky to do concisely. Consider a List things and method Optional resolve(Thing thing). I…
Yona Appletree
  • 8,801
  • 6
  • 35
  • 52
298
votes
12 answers

Sorting a list using Lambda/Linq to objects

I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects. Ex: public class Employee { public string FirstName {set; get;} public string LastName {set; get;} public DateTime DOB {set;…
DotnetDude
  • 11,617
  • 35
  • 100
  • 158
291
votes
4 answers

How to merge a list of lists with same type of items to a single list of items?

The question is confusing, but it is much more clear as described by the following code: List> listOfList; // add three lists of List to listOfList, for example /* listOfList = new { { 1, 2, 3}, // list 1 of 1, 3, and 3 …
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
290
votes
3 answers

Where do I mark a lambda expression async?

I've got this code: private async void ContextMenuForGroupRightTapped(object sender, RightTappedRoutedEventArgs args) { CheckBox ckbx = null; if (sender is CheckBox) { ckbx = sender as CheckBox; } if (null == ckbx) { …
286
votes
12 answers

`break` and `continue` in `forEach` in Kotlin

Kotlin has very nice iterating functions, like forEach or repeat, but I am not able to make the break and continue operators work with them (both local and non-local): repeat(5) { break } (1..5).forEach { continue@forEach } The goal is to…
voddan
  • 31,956
  • 8
  • 77
  • 87
285
votes
1 answer

How is "int main(){(([](){})());}" valid C++?

I recently came across the following esoteric piece of code. int main(){(([](){})());} Reformat it as follows to make it more readable: int main(){ (([](){})()); // Um... what?!?! } But I can't get my head around how (([](){})()) is valid…
Mysticial
  • 464,885
  • 45
  • 335
  • 332
277
votes
12 answers

Can lambda functions be templated?

In C++11, is there a way to template a lambda function? Or is it inherently too specific to be templated? I understand that I can define a classic templated class/functor instead, but the question is more like: does the language allow templating…
Klaim
  • 67,274
  • 36
  • 133
  • 188
276
votes
14 answers

convert a list of objects from one type to another using lambda expression

I have a foreach loop reading a list of objects of one type and producing a list of objects of a different type. I was told that a lambda expression can achieve the same result. var origList = List(); // assume populated var targetList =…
Stratton
  • 3,493
  • 3
  • 19
  • 9
265
votes
1 answer

How to remove a lambda event handler

I recently discovered that I can use lambdas to create simple event handlers. I could for example subscribe to a click event like this: button.Click += (s, e) => MessageBox.Show("Woho"); But how would you unsubscribe it?
Svish
  • 152,914
  • 173
  • 462
  • 620
264
votes
13 answers

How to use a Java8 lambda to sort a stream in reverse order?

I'm using java lambda to sort a list. how can I sort it in a reverse way? I saw this post, but I want to use java 8 lambda. Here is my code (I used * -1) as a hack Arrays.asList(files).stream() .filter(file -> isNameLikeBaseLine(file,…
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157