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, or Java. (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

26400 questions
1740
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
  • 135,113
  • 24
  • 223
  • 283
1594
votes
9 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
  • 327,095
  • 105
  • 629
  • 812
1021
votes
10 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
  • 10,764
  • 3
  • 19
  • 16
990
votes
22 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 Cammann
  • 14,873
  • 4
  • 32
  • 47
950
votes
26 answers

Why are Python 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…
meade
  • 21,435
  • 12
  • 29
  • 36
940
votes
15 answers

List comprehension vs. lambda + filter

I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items. My code looked like this: my_list = [x for x in my_list if x.attribute == value] But then I thought, wouldn't it be better…
Agos
  • 16,699
  • 9
  • 52
  • 68
866
votes
13 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
  • 15,764
  • 8
  • 35
  • 41
784
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
  • 21,296
  • 8
  • 50
  • 70
781
votes
18 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
  • 18,547
  • 8
  • 40
  • 59
598
votes
18 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
  • 74,179
  • 49
  • 153
  • 188
551
votes
23 answers

Finding the average of a list

I have to find the average of a list in Python. This is my code so far l = [15, 18, 2, 36, 12, 78, 5, 6, 9] print reduce(lambda x, y: x + y, l) I've got it so it adds together the values in the list, but I don't know how to make it divide into…
Carla Dessi
  • 7,828
  • 9
  • 33
  • 50
542
votes
22 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
  • 14,711
  • 10
  • 43
  • 75
525
votes
26 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
  • 20,107
  • 18
  • 63
  • 98
497
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
  • 4,989
  • 3
  • 14
  • 4
465
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,437
  • 3
  • 15
  • 20
1
2 3
99 100