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
175
votes
10 answers

Why doesn't print work in a lambda?

Why doesn't this work? lambda: print "x" Is this not a single statement, or is it something else? The documentation seems a little sparse on what is allowed in a lambda...
Anycorn
  • 50,217
  • 42
  • 167
  • 261
175
votes
3 answers

Built-in Java 8 predicate that always returns true?

Google Guava has a predicate that always returns true. Does Java 8 have something similar for its Predicate? I know I could use (foo)->{return true;}, but I want something pre-made, analogous to Collections.emptySet().
Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
174
votes
5 answers

How to serialize a lambda?

How can I elegantly serialize a lambda? For example, the code below throws a NotSerializableException. How can I fix it without creating a SerializableRunnable "dummy" interface? public static void main(String[] args) throws Exception { File…
assylias
  • 321,522
  • 82
  • 660
  • 783
172
votes
14 answers

Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambdas. Part 1 by Dhananjay Nene). This function : Creates a lazy and sequential…
artella
  • 5,068
  • 4
  • 27
  • 35
171
votes
14 answers

Java 8: Where is TriFunction (and kin) in java.util.function? Or what is the alternative?

I see java.util.function.BiFunction, so I can do this: BiFunction f = (x, y) -> { return 0; }; What if that is not good enough and I need TriFunction? It doesn't exist! TriFunction f =…
Richard Finegan
  • 1,895
  • 2
  • 12
  • 8
170
votes
5 answers

Performance of foreach, array_map with lambda and array_map with static function

What's the performance difference (if there is any) between these three approaches, both used to transform an array to another array? Using foreach Using array_map with lambda/closure function Using array_map with 'static' function/method Is there…
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
168
votes
10 answers

Copy a stream to avoid "stream has already been operated upon or closed"

I'd like to duplicate a Java 8 stream so that I can deal with it twice. I can collect as a list and get new streams from that; // doSomething() returns a stream List thing = doSomething().collect(toList()); thing.stream()... // do…
Toby
  • 9,523
  • 8
  • 36
  • 59
165
votes
4 answers

Lambda expression vs method reference

IntelliJ keeps proposing me to replace my lambda expressions with method references. Is there any objective difference between both of them?
Gerard
  • 2,784
  • 3
  • 19
  • 17
164
votes
5 answers

String.IsNullOrWhiteSpace in LINQ Expression

I have the following code: return this.ObjectContext.BranchCostDetails.Where( b => b.TarrifId == tariffId && b.Diameter == diameter || (b.TarrifId==tariffId && !string.IsNullOrWhiteSpace(b.Diameter)) || (!b.TarrifId.HasValue) &&…
Hossein Moradinia
  • 6,116
  • 14
  • 59
  • 85
164
votes
5 answers

Group by with multiple columns using lambda

How can I group by with multiple columns using lambda? I saw examples of how to do it using linq to entities, but I am looking for lambda form.
Naor
  • 23,465
  • 48
  • 152
  • 268
163
votes
1 answer

What is a 'SAM type' in Java?

Reading up on the Java-8 spec, I keep seeing references to 'SAM types'. I haven't been able to find a clear explanation of what this is. What is a SAM type and what is an example scenario of when one might be used?
Cody
  • 2,451
  • 2
  • 20
  • 19
158
votes
9 answers

Can a C# lambda expression have more than one statement?

Can a C# lambda expression include more than one statement? (Edit: As referenced in several of the answers below, this question originally asked about "lines" rather than "statements".)
paseena
  • 4,207
  • 6
  • 32
  • 51
158
votes
16 answers

How do I pronounce "=>" as used in lambda expressions in .Net

I very rarely meet any other programmers! My thought when I first saw the token was "implies that" since that's what it would read it as in a mathematical proof but that clearly isn't its sense. So how do I say or read "=>" as…
Christopher Edwards
  • 6,589
  • 8
  • 43
  • 57
157
votes
5 answers

How do I use the new computeIfAbsent function?

I very much want to use Map.computeIfAbsent but it has been too long since lambdas in undergrad. Almost directly from the docs: it gives an example of the old way to do things: Map whoLetDogsOut = new ConcurrentHashMap<>(); String…
Benjamin H
  • 5,164
  • 6
  • 34
  • 42
155
votes
6 answers

Optional orElse Optional in Java

I've been working with the new Optional type in Java 8, and I've come across what seems like a common operation that isn't supported functionally: an "orElseOptional" Consider the following pattern: Optional resultFromServiceA =…
Yona Appletree
  • 8,801
  • 6
  • 35
  • 52