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

Multiple Order By with LINQ

I start with a basic class that I want to manipulate in a List using LINQ, something like the following: public class FooBar { public virtual int Id { get; set; } public virtual string Foo { get; set; } public virtual string Bar…
sdanna
  • 2,969
  • 2
  • 19
  • 14
262
votes
6 answers

OrderBy descending in Lambda expression?

I know in normal Linq grammar, orderby xxx descending is very easy, but how do I do this in Lambda expression?
silent
  • 3,964
  • 5
  • 27
  • 29
260
votes
8 answers

python max function using 'key' and lambda expression

I come from OOP background and trying to learn python. I am using the max function which uses a lambda expression to return the instance of type Player having maximum totalScore among the list players. def winner(): w = max(players, key=lambda…
Vijay
  • 3,420
  • 4
  • 20
  • 21
251
votes
6 answers

Filter values only if not null using lambda in Java8

I have a list of objects say car. I want to filter this list based on some parameter using Java 8. But if the parameter is null, it throws NullPointerException. How to filter out null values? Current code is as follows requiredCars =…
vaibhavvc1092
  • 3,067
  • 4
  • 19
  • 26
238
votes
6 answers

Anonymous recursive PHP functions

Is it possible to have a PHP function that is both recursive and anonymous? This is my attempt to get it to work, but it doesn't pass in the function name. $factorial = function( $n ) use ( $factorial ) { if( $n <= 1 ) return 1; return…
Kendall Hopkins
  • 43,213
  • 17
  • 66
  • 89
233
votes
7 answers

Is it possible to type hint a lambda function?

Currently, in Python, a function's parameters and return types can be type hinted as follows: def func(var1: str, var2: str) -> int: return var1.index(var2) Which indicates that the function takes two strings, and returns an integer. However,…
Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
230
votes
25 answers

Java "lambda expressions not supported at this language level"

I was testing out some new features of Java 8 and copied the example into my IDE (Eclipse originally, then IntelliJ) as shown here Eclipse offered no support whatsoever for lambda expressions, and IntelliJ kept reporting an error Lambda…
Czipperz
  • 3,268
  • 2
  • 18
  • 25
228
votes
5 answers

Local function vs Lambda C# 7.0

I am looking at the new implementations in C# 7.0 and I find it interesting that they have implemented local functions but I cannot imagine a scenario where a local function would be preferred over a lambda expression, and what is the difference…
Sid
  • 14,176
  • 7
  • 40
  • 48
227
votes
9 answers

Variable used in lambda expression should be final or effectively final

Variable used in lambda expression should be final or effectively final When I try to use calTz it is showing this error. private TimeZone extractCalendarTimeZoneComponent(Calendar cal, TimeZone calTz) { try { …
user3610470
  • 2,271
  • 2
  • 12
  • 7
225
votes
11 answers

Max return value if empty query

I have this query: int maxShoeSize = Workers .Where(x => x.CompanyId == 8) .Max(x => x.ShoeSize); What will be in maxShoeSize if company 8 has no workers at all? UPDATE: How can I change the query in order to get 0 and not an exception?
Naor
  • 23,465
  • 48
  • 152
  • 268
222
votes
8 answers

Lambda capture as const reference?

Is it possible to capture by const reference in a lambda expression? I want the assignment marked below to fail, for example: #include #include using namespace std; int main() { string strings[] = { "hello", …
John Dibling
  • 99,718
  • 31
  • 186
  • 324
218
votes
3 answers

Does a lambda expression create an object on the heap every time it's executed?

When I iterate over a collection using the new syntactic sugar of Java 8, such as myStream.forEach(item -> { // do something useful }); Isn't this equivalent to the 'old syntax' snippet below? myStream.forEach(new Consumer() { @Override …
Bastian Voigt
  • 5,311
  • 6
  • 47
  • 65
218
votes
10 answers

Does Java SE 8 have Pairs or Tuples?

I am playing around with lazy functional operations in Java SE 8, and I want to map an index i to a pair / tuple (i, value[i]), then filter based on the second value[i] element, and finally output just the indices. Must I still suffer this: What is…
necromancer
  • 23,916
  • 22
  • 68
  • 115
213
votes
15 answers

Recursive lambda functions in C++11

I am new to C++11. I am writing the following recursive lambda function, but it doesn't compile. sum.cpp #include #include auto term = [](int a)->int { return a*a; }; auto next = [](int a)->int { return ++a; }; auto…
weima
  • 4,653
  • 6
  • 34
  • 55
212
votes
10 answers

Syntax behind sorted(key=lambda: ...)

I don't quite understand the syntax behind the sorted() argument: key=lambda variable: variable[0] Isn't lambda arbitrary? Why is variable stated twice in what looks like a dict?
Christopher Markieta
  • 5,674
  • 10
  • 43
  • 60