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
1855
votes
4 answers

Is there a reason for C#'s reuse of the variable in a foreach?

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. For example: foreach (var s in strings) { query = query.Where(i => i.Prop == s); // access to modified closure ... } Due…
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
1749
votes
10 answers

What is a lambda expression in C++11?

What is a lambda expression in C++11? When would I use one? What class of problem do they solve that wasn't possible prior to their introduction? A few examples, and use cases would be useful.
Nawaz
  • 353,942
  • 115
  • 666
  • 851
1136
votes
12 answers

Why would you use Expression> rather than Func?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression> rather than a plain old Func?
Richard Nagle
  • 11,974
  • 3
  • 21
  • 16
1089
votes
17 answers

List comprehension vs. lambda + filter

I have a list that I want to filter by an attribute of the items. Which of the following is preferred (readability, performance, other reasons)? xs = [x for x in xs if x.attribute == value] xs = filter(lambda x: x.attribute == value, xs)
Agos
  • 18,542
  • 11
  • 56
  • 70
1042
votes
23 answers

Java 8 List into Map

I want to translate a List of objects into a Map using Java 8's streams and lambdas. This is how I would write it in Java 7 and below. private Map nameMap(List choices) { final Map hashMap = new…
Tom
  • 15,798
  • 4
  • 37
  • 48
981
votes
26 answers

How are lambdas useful?

I'm trying to figure out Python lambdas. Is lambda one of those "interesting" language items that in real life should be forgotten? I'm sure there are some edge cases where it might be needed, but given the obscurity of it, the potential of it being…
meade
  • 22,875
  • 12
  • 32
  • 36
975
votes
16 answers

What is the difference between a 'closure' and a 'lambda'?

Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused. And now that we're here, how do they differ from a regular function?
sker
  • 17,842
  • 8
  • 37
  • 41
845
votes
23 answers

What is a lambda (function)?

For a person without a comp-sci background, what is a lambda in the world of Computer Science?
Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
833
votes
20 answers

Distinct() with lambda?

Right, so I have an enumerable and wish to get distinct values from it. Using System.Linq, there's, of course, an extension method called Distinct. In the simple case, it can be used with no parameters, like: var distinctValues =…
Tor Haugen
  • 19,509
  • 9
  • 45
  • 63
656
votes
19 answers

Getting all types that implement an interface

Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations? This is what I want to re-write: foreach (Type t in this.GetType().Assembly.GetTypes()) if (t is…
juan
  • 80,295
  • 52
  • 162
  • 195
622
votes
28 answers

Java 8 Lambda function that throws exception?

I know how to create a reference to a method that has a String parameter and returns an int, it's: Function However, this doesn't work if the function throws an exception, say it's defined as: Integer myMethod(String s) throws…
Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
593
votes
23 answers

Retrieving Property name from lambda expression

Is there a better way to get the Property name when passed in via a lambda expression? Here is what i currently have. eg. GetSortingInfo(u => u.UserId); It worked by casting it as a memberexpression only when the property was a string.…
Schotime
  • 15,707
  • 10
  • 46
  • 75
556
votes
10 answers

Join/Where with LINQ and Lambda

I'm having trouble with a query written in LINQ and Lambda. So far, I'm getting a lot of errors here's my code: int id = 1; var query = database.Posts.Join(database.Post_Metas, post => database.Posts.Where(x => x.ID…
David
  • 5,579
  • 3
  • 16
  • 4
498
votes
16 answers

Is there a way to perform "if" in python's lambda?

In Python 2.6, I want to do: f = lambda x: if x==2 print x else raise Exception() f(2) #should print "2" f(3) #should throw an exception This clearly isn't the syntax. Is it possible to perform an if in lambda and if so how to do it?
Guy
  • 14,178
  • 27
  • 67
  • 88
496
votes
9 answers

When should I use arrow functions in ECMAScript 6?

With () => {} and function () {} we are getting two very similar ways to write functions in ES6. In other languages lambda functions often distinguish themselves by being anonymous, but in ECMAScript any function can be anonymous. Each of the two…
1
2 3
99 100