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
137
votes
5 answers

Proper way to receive a lambda as parameter by reference

What is the right way to define a function that receives a int->int lambda parameter by reference? void f(std::function< int(int) >& lambda); or void f(auto& lambda); I'm not sure the last form is even legal syntax. Are there other ways to define…
lurscher
  • 25,930
  • 29
  • 122
  • 185
137
votes
3 answers

How to check if element exists using a lambda expression?

Specifically, I have TabPane, and I would like to know if there is element with specific ID in it. So, I would like to do this with lambda expression in Java: boolean idExists = false; String idToCheck = "someId"; for (Tab t : tabPane.getTabs()){ …
Miljac
  • 2,045
  • 4
  • 19
  • 28
136
votes
16 answers

Which is more preferable to use: lambda functions or nested functions ('def')?

I mostly use lambda functions but sometimes use nested functions that seem to provide the same behavior. Here are some trivial examples where they functionally do the same thing if either were found within another function: Lambda function >>> a =…
Ray
  • 187,153
  • 97
  • 222
  • 204
136
votes
7 answers

How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)

EDITED: How can I set a Django field's default to a function that gets evaluated each time a new model object gets created? I want to do something like the following, except that in this code, the code gets evaluated once and sets the default to the…
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
135
votes
2 answers

Is this object-lifetime-extending-closure a C# compiler bug?

I was answering a question about the possibility of closures (legitimately) extending object-lifetimes when I ran into some extremely curious code-gen on the part of the C# compiler (4.0 if that matters). The shortest repro I can find is the…
Ani
  • 111,048
  • 26
  • 262
  • 307
134
votes
9 answers

Uses of Action delegate in C#

I was working with the Action Delegates in C# in the hope of learning more about them and thinking where they might be useful. Has anybody used the Action Delegate, and if so why? or could you give some examples where it might be useful?
Biswanath
  • 9,075
  • 12
  • 44
  • 58
134
votes
4 answers

How does generic lambda work in C++14?

How does generic lambda work (auto keyword as an argument type) in C++14 standard? Is it based on C++ templates where for each different argument type compiler generates a new function with the same body but replaced types (compile-time…
sasha.sochka
  • 14,395
  • 10
  • 44
  • 68
132
votes
10 answers

How can I get every nth item from a List?

I'm using .NET 3.5 and would like to be able to obtain every *n*th item from a List. I'm not bothered as to whether it's achieved using a lambda expression or LINQ. Edit Looks like this question provoked quite a lot of debate (which is a good…
Paul Suart
  • 6,505
  • 7
  • 44
  • 65
132
votes
5 answers

Cannot convert lambda expression to type 'string' because it is not a delegate type

I am using a LINQ lambda expression like so: int Value = 1; qryContent objContentLine; using (Entities db = new Entities()) { objContentLine = (from q in db.qryContents where q.LineID == Value …
Deep Sharma
  • 3,374
  • 3
  • 29
  • 49
131
votes
9 answers

Why must a lambda expression be cast when supplied as a plain Delegate parameter

Take the method System.Windows.Forms.Control.Invoke(Delegate method) Why does this give a compile time error: string str = "woop"; Invoke(() => this.Text = str); // Error: Cannot convert lambda expression to type 'System.Delegate' // because it is…
xyz
  • 27,223
  • 29
  • 105
  • 125
130
votes
14 answers

Java 8 lambda get and remove element from list

Given a list of elements, I want to get the element with a given property and remove it from the list. The best solution I found is: ProducerDTO p = producersProcedureActive .stream() .filter(producer ->…
Marco Stramezzi
  • 2,143
  • 4
  • 17
  • 37
130
votes
4 answers

C# Pass Lambda Expression as Method Parameter

I have a lambda expression that I'd like to be able to pass around and reuse. Here's the code: public List getJobs(/* i want to pass the lambda expr in here */) { using (SqlConnection connection = new SqlConnection(getConnectionString()))…
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
129
votes
6 answers

Lambda returning itself: is this legal?

Consider this fairly useless program: #include int main(int argc, char* argv[]) { int a = 5; auto it = [&](auto self) { return [&](auto b) { std::cout << (a + b) << std::endl; return self(self); }; }; …
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
127
votes
4 answers

Using lambda expressions for event handlers

I currently have a page which is declared as follows: public partial class MyPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //snip MyButton.Click += (o, i) => { //snip …
Christopher Garcia
  • 2,536
  • 7
  • 30
  • 40
127
votes
3 answers

What is the purpose of std::function and how to use it?

It is necessary for me to use std::function but I don't know what the following syntax means. std::function f_name = []() { FNAME(); }; What is the goal of using std::function? Is it to make a pointer to a function?
user2982229
  • 1,395
  • 2
  • 11
  • 13