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
212
votes
8 answers

Can a java lambda have more than 1 parameter?

In Java, is it possible to have a lambda accept multiple different types? I.e: Single variable works: Function adder = i -> i + 1; System.out.println (adder.apply (10)); Varargs also work: Function
Leo Ufimtsev
  • 6,240
  • 5
  • 40
  • 48
209
votes
6 answers

Cannot use ref or out parameter in lambda expressions

Why can't you use a ref or out parameter in a lambda expression? I came across the error today and found a workaround but I was still curious why this is a compile-time error. CS1628: Cannot use in ref or out parameter 'parameter' inside an…
skalb
  • 5,357
  • 3
  • 26
  • 23
208
votes
7 answers

Move capture in lambda

How do I capture by move (also known as rvalue reference) in a C++11 lambda? I am trying to write something like this: std::unique_ptr myPointer(new int); std::function example = [std::move(myPointer)]{ *myPointer = 4; };
user406009
206
votes
5 answers

In C#, What is a monad?

There is a lot of talk about monads these days. I have read a few articles / blog posts, but I can't go far enough with their examples to fully grasp the concept. The reason is that monads are a functional language concept, and thus the examples are…
Charlie Flowers
  • 17,338
  • 10
  • 71
  • 88
205
votes
4 answers

Using data member in lambda capture list inside a member function

The following code compiles with gcc 4.5.1 but not with VS2010 SP1: #include #include #include #include #include #include using namespace std; class puzzle { vector> grid; …
vivek
  • 4,951
  • 4
  • 25
  • 33
205
votes
15 answers

Java 8: Lambda-Streams, Filter by Method with Exception

I have a problem trying out the Lambda expressions of Java 8. Usually it works fine, but now I have methods that throw IOException's. It's best if you look at the following code: class Bank{ .... public Set getActiveAccountNumbers()…
Martin Weber
  • 3,892
  • 4
  • 20
  • 23
202
votes
8 answers

In Java 8 how do I transform a Map to another Map using a lambda?

I've just started looking at Java 8 and to try out lambdas I thought I'd try to rewrite a very simple thing I wrote recently. I need to turn a Map of String to Column into another Map of String to Column where the Column in the new Map is a…
annesadleir
  • 2,141
  • 2
  • 12
  • 8
199
votes
10 answers

Modifying local variable from inside lambda

Modifying a local variable in forEach gives a compile error: Normal int ordinal = 0; for (Example s : list) { s.setOrdinal(ordinal); ordinal++; } With Lambda int ordinal = 0; list.forEach(s -> { …
Patan
  • 17,073
  • 36
  • 124
  • 198
198
votes
7 answers

How to convert a String to its equivalent LINQ Expression Tree?

This is a simplified version of the original problem. I have a class called Person: public class Person { public string Name { get; set; } public int Age { get; set; } public int Weight { get; set; } public DateTime FavouriteDay { get; set;…
Codebrain
  • 5,565
  • 4
  • 28
  • 21
198
votes
4 answers

How to sort with a lambda?

sort(mMyClassVector.begin(), mMyClassVector.end(), [](const MyClass & a, const MyClass & b) { return a.mProperty > b.mProperty; }); I'd like to use a lambda function to sort custom classes in place of binding an instance method. However,…
BTR
  • 4,880
  • 4
  • 24
  • 21
195
votes
3 answers

Why can lambdas be better optimized by the compiler than plain functions?

In his book The C++ Standard Library (Second Edition) Nicolai Josuttis states that lambdas can be better optimized by the compiler than plain functions. In addition, C++ compilers optimize lambdas better than they do ordinary functions. (Page…
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
193
votes
6 answers

delegate keyword vs. lambda notation

Once it is compiled, is there a difference between: delegate { x = 0; } and () => { x = 0 } ?
MojoFilter
  • 12,256
  • 14
  • 53
  • 61
189
votes
11 answers

What are functional interfaces used for in Java 8?

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions. Java 8 provides some built-in functional interfaces and if we want to define any functional interface then we can make…
Madhusudan
  • 4,637
  • 12
  • 55
  • 86
188
votes
6 answers

LINQ: "contains" and a Lambda query

I have a List called buildingStatus. I'd like to check whether it contains a status whose char code (returned by GetCharCode()) equals some variable, v.Status. Is there some way of doing this, along the lines of the (non-compiling)…
mark smith
  • 20,637
  • 47
  • 135
  • 187
183
votes
9 answers

What is the type of lambda when deduced with "auto" in C++11?

I had a perception that, type of a lambda is a function pointer. When I performed following test, I found it to be wrong (demo). #define LAMBDA [] (int i) -> long { return 0; } int main () { long (*pFptr)(int) = LAMBDA; // ok auto pAuto =…
iammilind
  • 68,093
  • 33
  • 169
  • 336