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

Why can't an anonymous method be assigned to var?

I have the following code: Func comparer = delegate(string value) { return value != "0"; }; However, the following does not compile: var comparer = delegate(string value) { return value != "0"; }; Why can't the compiler…
Marlon
  • 19,924
  • 12
  • 70
  • 101
150
votes
4 answers

Understanding Spliterator, Collector and Stream in Java 8

I am having trouble understanding the Stream interface in Java 8, especially where it has to do with the Spliterator and Collector interfaces. My problem is that I simply can't understand Spliterator and the Collector interfaces yet, and as a…
149
votes
4 answers

Comparator.reversed() does not compile using lambda

I have a list with some User objects and i'm trying to sort the list, but only works using method reference, with lambda expression the compiler gives an error: List userList = Arrays.asList(u1, u2, u3); userList.sort(Comparator.comparing(u ->…
Andrey
  • 2,485
  • 3
  • 21
  • 26
148
votes
3 answers

Explicit Return Type of Lambda

When I try and compile this code (VS2010) I am getting the following error: error C3499: a lambda that has been specified to have a void return type cannot return a value void DataFile::removeComments() { string::const_iterator start, end; …
Ryan
  • 6,432
  • 7
  • 40
  • 54
147
votes
5 answers

Is it possible to figure out the parameter type and return type of a lambda?

Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how? Basically, I want lambda_traits which can be used in following ways: auto lambda = [](int i) { return long(i*10);…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
147
votes
13 answers

Assignment inside lambda expression in Python

I have a list of objects and I want to remove all objects that are empty except for one, using filter and a lambda expression. For example if the input is: [Object(name=""), Object(name="fake_name"), Object(name="")] ...then the output should…
Cat
  • 7,042
  • 8
  • 34
  • 36
145
votes
5 answers

Proper usage of Optional.ifPresent()

I am trying to understand the ifPresent() method of the Optional API in Java 8. I have simple logic: Optional user=... user.ifPresent(doSomethingWithUser(user.get())); But this results in a compilation error:…
rayman
  • 20,786
  • 45
  • 148
  • 246
144
votes
4 answers

Order a List (C#) by many fields?

I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order: object1 => A = "a", B = "h" object2 => A =…
Esabe
  • 1,941
  • 4
  • 16
  • 19
143
votes
4 answers

Lambda implicit capture fails with variable declared from structured binding

With the following code, I get a compile error C2065 'a': undeclared identifier (using visual studio 2017): [] { auto [a, b] = [] {return std::make_tuple(1, 2); }(); auto r = [&] {return a; }(); //error C2065 }(); However, the following…
143
votes
6 answers

How to debug stream().map(...) with lambda expressions?

In our project we are migrating to java 8 and we are testing the new features of it. On my project I'm using Guava predicates and functions to filter and transform some collections using Collections2.transform and Collections2.filter. On this…
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
142
votes
9 answers

converting a .net Func to a .net Expression>

Going from a lambda to an Expression is easy using a method call... public void GimmeExpression(Expression> expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { …
Dave Cameron
  • 2,110
  • 2
  • 19
  • 23
142
votes
5 answers

How to create an instance of anonymous interface in Kotlin?

I have a third party Java library which an object with interface like this: public interface Handler { void call(C context) throws Exception; } How can I concisely implement it in Kotlin similar to Java anonymous class like…
Peter Lamberg
  • 8,151
  • 3
  • 55
  • 69
141
votes
7 answers

Is there a way to specify an "empty" C# lambda expression?

I'd like to declare an "empty" lambda expression that does, well, nothing. Is there a way to do something like this without needing the DoNothing() method? public MyViewModel() { SomeMenuCommand = new RelayCommand( x => DoNothing(), …
Rob
  • 25,984
  • 32
  • 109
  • 155
140
votes
3 answers

C# lambda expression reverse direction <=

I have seen some code which uses the <= operator. Can you explain what is the use of having lambda in reverse direction?
user215675
  • 4,991
  • 9
  • 36
  • 40
138
votes
2 answers

Do c++11 lambdas capture variables they don't use?

When I use [=] to indicate that I would like all local variables to be captured by value in a lambda, will that result in all local variables in the function being copied, or just all local variables that are used by the lambda? So, for example, if…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194