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.)
lambda
3,783
questions
376
votes
8
answers
89k
views
What do lambda function closures capture? [duplicate]
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]:
...
224
votes
9
answers
110k
views
Creating functions (or lambdas) in a loop (or comprehension)
I'm trying to create functions inside of a loop:
functions = []
for i in range(3):
def f():
return i
functions.append(f)
Alternatively, with lambda:
functions = []
for i in range(3):...
1763
votes
11
answers
677k
views
What is a lambda expression, and when should I use one?
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.
985
votes
26
answers
591k
views
How are lambdas useful? [closed]
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 ...
507
votes
9
answers
150k
views
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 ...
353
votes
10
answers
287k
views
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 = ...
605
votes
23
answers
291k
views
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<User>(u => u.UserId);
It worked by casting it as a ...
1866
votes
4
answers
122k
views
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 ...
173
votes
14
answers
109k
views
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)...
327
votes
10
answers
160k
views
Combining two expressions (Expression<Func<T, bool>>)
I have two expressions of type Expression<Func<T, bool>> and I want to take to OR, AND or NOT of these and get a new expression of the same type
Expression<Func<T, bool>> ...
1150
votes
12
answers
310k
views
Why would you use Expression<Func<T>> rather than Func<T>?
I understand lambdas and the Func and Action delegates. But expressions
stump me.
In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>?
848
votes
24
answers
400k
views
What is a lambda (function)?
For a person without a comp-sci background, what is a lambda in the world of Computer Science?
666
votes
19
answers
400k
views
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....
393
votes
15
answers
209k
views
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 ...
1134
votes
20
answers
861k
views
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 = ...