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
15
votes
1 answer

using out of scope variables in C++11 lambda expressions

I'm playing with C++11 for fun. I'm wondering why this happens: //... std::vector agents; P_CommunicationProtocol requestPacket; //... bool repeated = std::any_of(agents.begin(), agents.end(), [](P_EndPoint i)->bool …
sorush-r
  • 10,490
  • 17
  • 89
  • 173
15
votes
2 answers

What's the story with ExpressionType.Assign?

I was under the impression that assignment was not possible inside a lambda expression. E.g., the following (admittedly not very useful) code Expression> expr = (x, y) => y = x; Produces the compiler error An expression tree may…
Dan Barowy
  • 2,270
  • 24
  • 35
15
votes
3 answers

Action to Delegate : new Action or casting Action?

I found two different ways to initialize a Delegate with an Action : Create a new action or casting to Action. Delegate foo = new Action(() => DoNothing(param)); Delegate bar = (Action)(() => DoNothing(param)); Is there a difference between this 2…
Hyralex
  • 990
  • 1
  • 8
  • 25
15
votes
3 answers

TypeError: () takes no arguments (1 given)

I am a newbie to python programming and am still trying to figure out the use of lambda. Was worrking on some gui program after much googling i figured that i need to use this for buttons to work as i need it to THIS WORKS mtrf = Button(root, text =…
evolutionizer
  • 283
  • 2
  • 3
  • 7
15
votes
1 answer

C# Lambda Functions: returning data

Am I missing something or is it not possible to return a value from a lambda function such as.. Object test = () => { return new Object(); }; or string test = () => { return "hello"; }; I get a build error "Cannot convert lambda expression to type…
Drew R
  • 2,988
  • 3
  • 19
  • 27
15
votes
1 answer

How do I compare two lambda expressions?

Possible Duplicate: How to check if two Expression> are the same I need to compare two lambda expressions, to check equality. Basicly, the two following lambda are identical: Expression> exp1 = (Foo f) =>…
Jonathas Costa
  • 1,006
  • 9
  • 27
15
votes
1 answer

Lambdas and generics in Java 8

I'm playing with future java 8 release aka JDK 1.8. And I found out that you can easily do interface Foo { int method(); } and use it like Foo foo = () -> 3; System.out.println("foo.method(); = " + foo.method()); which simply prints 3. And I also…
Natan Cox
  • 1,495
  • 2
  • 15
  • 28
15
votes
2 answers

List.Sort with lambda expression

I'm trying to sort part of a list with a lambda expression, but I get an error when trying to do so: List list = new List(); list.Add(1); list.Add(3); list.Add(2); list.Add(4); // works fine list.Sort((i1, i2) => i1.CompareTo(i2) ); //…
user673679
  • 1,327
  • 1
  • 16
  • 35
15
votes
2 answers

ternary operator doesn't work with lambda functions

I am assigning to a std::function a lambda expression. This snippet works if(fn_type==exponential) k.*variable = [=,&k](){ return initial*exp(-k.kstep*par); }; else k.*variable = [=,&k](){ return initial*pow(k.kstep, par);…
Lorenzo Pistone
  • 5,028
  • 3
  • 34
  • 65
15
votes
4 answers

C++11: In what order are lambda captures destructed?

Let's say I have two local smart pointers, foo and bar. shared_ptr foo = ... shared_ptr bar = ... These smart pointers are wrappers around resources that for some reason must be destructed in the order foo, then bar. Now I want to create…
Daniel Wolf
  • 12,855
  • 13
  • 54
  • 80
15
votes
11 answers

Can you reverse order a string in one line with LINQ or a LAMBDA expression

Not that I would want to use this practically (for many reasons) but out of strict curiousity I would like to know if there is a way to reverse order a string using LINQ and/or LAMBDA expressions in one line of code, without utilising any framework…
Student for Life
  • 1,023
  • 1
  • 9
  • 18
15
votes
1 answer

How to join multiple tables?

I have the following classes. I have a object var of Description class. I want to select Balance related to the Client provided in the var object using Linq to Sql or Lambda expression. How to join these tables to get the Balance from Account…
nebula
  • 3,932
  • 13
  • 53
  • 82
15
votes
2 answers

Lambda expressions don't work in Java 8?

I have a virtual machine running Windows XP SP3 32-bit. On this machine I installed the Java SE JDK 8 build b44 Developer Preview from here. I also installed the JavaFX 2.1 SDK. It works fine: java -version > java version "1.8.0-ea" > Java(TM) SE…
Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
15
votes
2 answers

create a lambda function from a string **properly**

Given a string such as "2*(i+j) <= 100" I want to generate the corresponding lambda function, fn = lambda i,j: 2*(i+j) <= 100 I can do this with eval, but I am seeking a less evil method. I have found import ast f = ast.Lambda('i,j', '2*(i+j) <=…
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
15
votes
3 answers

Anonymous function C++

I am trying to use the function signal(int,void(*)(int)) from to handle the floating point exception SIGFPE. I'd like to be able to print some useful diagnostics besides just a message saying "Floating point exception" or something to that…
Dan
  • 12,857
  • 7
  • 40
  • 57