3,894 questions
388
votes
8
answers
98k
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]:
...
239
votes
9
answers
117k
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):...
1778
votes
11
answers
716k
views
What is a lambda expression, and when should I use one?
What is a C++ lambda expression? Why does C++ have them, what problems do they solve that were not solvable prior to their addition? And how can I benifit from using them?
Please provide an example or ...
986
votes
26
answers
597k
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 ...
370
votes
12
answers
314k
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 = ...
515
votes
9
answers
158k
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 ...
609
votes
23
answers
298k
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 ...
1874
votes
4
answers
131k
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
117k
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)...
336
votes
10
answers
167k
views
Combining two expressions (Expression<Func<T, bool>>)
I have two expressions of type Expression<Func<T, bool>> and I want to take the OR, AND, or NOT of these and get a new expression of the same type.
Expression<Func<T, bool>> ...
1160
votes
12
answers
320k
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>?
398
votes
15
answers
216k
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 ...
851
votes
24
answers
414k
views
What is a lambda (function)?
For a person without a comp-sci background, what is a lambda in the world of Computer Science?
676
votes
19
answers
417k
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....
1168
votes
19
answers
962k
views
List comprehension vs. lambda + filter [closed]
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 = ...