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
493
votes
15 answers

Retrieving a List from a java.util.stream.Stream in Java 8

I was playing around with Java 8 lambdas to easily filter collections. But I did not find a concise way to retrieve the result as a new list within the same statement. Here is my most concise approach so far: List sourceLongList =…
Daniel K.
  • 5,747
  • 3
  • 19
  • 22
485
votes
23 answers

No Multiline Lambda in Python: Why not?

I've heard it said that multiline lambdas can't be added in Python because they would clash syntactically with the other syntax constructs in Python. I was thinking about this on the bus today and realized I couldn't think of a single Python…
Imagist
  • 18,086
  • 12
  • 58
  • 77
484
votes
10 answers

List OrderBy Alphabetical Order

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List. For the sake of this example, let's say I have a List of a Person type with a property of lastname. How would I sort this List using a lambda expression? List
SaaS Developer
  • 9,835
  • 7
  • 34
  • 45
482
votes
16 answers

How do I define a method which takes a lambda as a parameter in Java 8?

In Java 8, methods can be created as Lambda expressions and can be passed by reference (with a little work under the hood). There are plenty of examples online with lambdas being created and used with methods, but no examples of how to make a method…
Marius
  • 57,995
  • 32
  • 132
  • 151
433
votes
21 answers

Reflecting parameter name: abuse of C# lambda expressions or syntax brilliance?

I am looking at the MvcContrib Grid component and I'm fascinated, yet at the same time repulsed, by a syntactic trick used in the Grid syntax: .Attributes(style => "width:100%") The syntax above sets the style attribute of the generated HTML to…
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
417
votes
15 answers

Break or return from Java 8 stream forEach?

When using external iteration over an Iterable we use break or return from enhanced for-each loop as: for (SomeObject obj : someObjects) { if (some_condition_met) { break; // or return obj } } How can we break or return using the…
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331
386
votes
15 answers

Difference between final and effectively final

I'm playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know that when I use variables inside anonymous class they must be final in outer class, but still…
alex
  • 10,900
  • 15
  • 70
  • 100
367
votes
8 answers

What do lambda function closures capture?

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders=[None, None, None, None] for i in [0,1,2,3]: adders[i]=lambda a: i+a print adders[1](3) It builds…
Boaz
  • 25,331
  • 21
  • 69
  • 77
365
votes
18 answers

How can I throw CHECKED exceptions from inside Java 8 lambdas/streams?

How can I throw CHECKED exceptions from inside Java 8 lambda, used in a stream for example? In other words, I want to make code like this compile: public List getClasses() throws ClassNotFoundException { List classes = …
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
361
votes
3 answers

Java 8 lambdas, Function.identity() or t->t

I have a question regarding the usage of the Function.identity() method. Imagine the following code: Arrays.asList("a", "b", "c") .stream() .map(Function.identity()) // <- This, .map(str -> str) // <- is the…
user4464654
345
votes
14 answers

When to use lambda, when to use Proc.new?

In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and Proc.new on the other. What are those differences? Can you give guidelines on how to decide which one to choose? In Ruby 1.9, proc and lambda are different. What's…
Michiel de Mare
  • 41,982
  • 29
  • 103
  • 134
334
votes
10 answers

Java 8 lambda Void argument

Let's say I have the following functional interface in Java 8: interface Action { U execute(T t); } And for some cases I need an action without arguments or return type. So I write something like this: Action a = () -> {…
Wickoo
  • 6,745
  • 5
  • 32
  • 45
331
votes
10 answers

Passing capturing lambda as function pointer

Is it possible to pass a lambda function as a function pointer? If so, I must be doing something incorrectly because I am getting a compile error. Consider the following example using DecisionFn = bool(*)(); class Decide { public: …
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
327
votes
4 answers

Java 8 Streams: multiple filters vs. complex condition

Sometimes you want to filter a Stream with more than one condition: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) ... or you could do the same with a complex condition and a single filter: myList.stream().filter(x -> x.size() >…
deamon
  • 89,107
  • 111
  • 320
  • 448
327
votes
24 answers

Filter Java Stream to 1 and only 1 element

I am trying to use Java 8 Streams to find elements in a LinkedList. I want to guarantee, however, that there is one and only one match to the filter criteria. Take this code: public static void main(String[] args) { LinkedList users = new…
ryvantage
  • 13,064
  • 15
  • 63
  • 112