Skip to main content
Filter by
Sorted by
Tagged with
1871 votes
4 answers
124k views

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 ...
StriplingWarrior's user avatar
1776 votes
11 answers
708k views

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 ...
Sarfaraz Nawaz's user avatar
1165 votes
19 answers
955k views

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 = ...
Agos's user avatar
  • 19.6k
1159 votes
12 answers
320k views

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>?
Richard Nagle's user avatar
1075 votes
23 answers
815k views

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<String, Choice> nameMap(List<Choice> ...
Tom's user avatar
  • 16.3k
1002 votes
18 answers
195k views

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's user avatar
  • 18.5k
986 votes
26 answers
597k views

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 ...
meade's user avatar
  • 23.4k
852 votes
24 answers
413k views

For a person without a comp-sci background, what is a lambda in the world of Computer Science?
Brian Warshaw's user avatar
839 votes
20 answers
483k views

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 ...
Tor Haugen's user avatar
  • 19.7k
676 votes
19 answers
416k views

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....
juan's user avatar
  • 82.3k
651 votes
28 answers
553k views

I know how to create a reference to a method that has a String parameter and returns an int, it's: Function<String, Integer> However, this doesn't work if the function throws an exception, say ...
Rocky Pulley's user avatar
  • 23.4k
608 votes
23 answers
298k views

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 ...
Schotime's user avatar
  • 16.1k
569 votes
10 answers
1.3m views

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 => ...
David's user avatar
  • 5,709
529 votes
16 answers
904k views

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 ...
Guy's user avatar
  • 14.9k
519 votes
26 answers
325k views

I've heard it said that multiline lambdas can't be added in Python because they would clash syntactically with the other syntax constructs in Python. I was thinking about this on the bus today and ...
Imagist's user avatar
  • 18.6k
515 votes
9 answers
157k views

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 ...
lyschoening's user avatar
  • 18.8k
512 votes
15 answers
485k views

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 ...
Daniel K.'s user avatar
  • 5,947
505 votes
16 answers
444k views

In Java 8, methods can be created as Lambda expressions and can be passed by reference (with a little work under the hood). There are plenty of examples online with lambdas being created and used with ...
Marius's user avatar
  • 59.2k
489 votes
10 answers
815k views

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<T>. For the sake of this example, let's say I have a List of a Person type with a property of lastname. How would I ...
SaaS Developer's user avatar
435 votes
15 answers
535k views

When using external iteration over an Iterable we use break or return from enhanced for-each loop as: for (SomeObject obj : someObjects) { if (some_condition_met) { break; // or return obj ...
Tapas Bose's user avatar
434 votes
21 answers
30k views

I am looking at the MvcContrib Grid component and I'm fascinated, yet at the same time repulsed, by a syntactic trick used in the Grid syntax: .Attributes(style => "width:100%") The syntax above ...
398 votes
15 answers
215k views

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 ...
alex's user avatar
  • 11.4k
387 votes
4 answers
271k views

I have a question regarding the usage of the Function.identity() method. Imagine the following code: Arrays.asList("a", "b", "c") .stream() .map(Function.identity()) // <- ...
user avatar
386 votes
8 answers
98k views

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]: ...
Boaz's user avatar
  • 26.3k
378 votes
18 answers
170k views

How can I throw checked exceptions from inside Java 8 lambda, used in a stream for example? In other words, I want to make code like this compile: public List<Class> getClasses() throws ...
Marcelo Glasberg's user avatar
373 votes
10 answers
328k views

Let's say I have the following functional interface in Java 8: interface Action<T, U> { U execute(T t); } And for some cases I need an action without arguments or return type. So I write ...
Wickoo's user avatar
  • 7,437
370 votes
12 answers
312k views

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 = ...
Cory Kramer's user avatar
358 votes
8 answers
154k views

I get this pep8 warning whenever I use lambda expressions. Are lambda expressions not recommended? If not why?
Kechit Goyal's user avatar
  • 4,312
346 votes
26 answers
400k views

I am trying to use Java 8 Streams to find elements in a LinkedList. I want to guarantee, however, that there is one and only one match to the filter criteria. Take this code: public static void main(...
ryvantage's user avatar
  • 13.6k
346 votes
14 answers
85k views

In Ruby 1.8, there are subtle differences between proc/lambda on the one hand, and Proc.new on the other. What are those differences? Can you give guidelines on how to decide which one to choose? In ...
Michiel de Mare's user avatar
346 votes
1 answer
51k views

In Stack Overflow question Redefining lambdas not allowed in C++11, why?, a small program was given that does not compile: int main() { auto test = []{}; test = []{}; } The question was ...
Daniel Frey's user avatar
  • 57.2k
343 votes
13 answers
133k views

Short example: #include <iostream> int main() { int n; [&](){n = 10;}(); // OK [=]() mutable {n = 20;}(); // OK // [=](){n = 10;}(); // Error: a by-...
kizzx2's user avatar
  • 19.3k
341 votes
4 answers
311k views

Sometimes you want to filter a Stream with more than one condition: myList.stream().filter(x -> x.size() > 10).filter(x -> x.isCool()) ... or you could do the same with a complex condition ...
deamon's user avatar
  • 93.1k
335 votes
10 answers
167k views

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>> ...
BjartN's user avatar
  • 5,537
327 votes
12 answers
277k views

Kotlin has very nice iterating functions, like forEach or repeat, but I am not able to make the break and continue operators work with them (both local and non-local): repeat(5) { break } (1..5)....
voddan's user avatar
  • 34.1k
321 votes
16 answers
255k views

I have quickly read over the Microsoft Lambda Expression documentation. This kind of example has helped me to understand better, though: delegate int del(int i); del myDelegate = x => x * x; int ...
Patrick Desjardins's user avatar
319 votes
14 answers
225k views

In C++11, is there a way to template a lambda function? Or is it inherently too specific to be templated? I understand that I can define a classic templated class/functor instead, but the question is ...
Klaim's user avatar
  • 70.3k
319 votes
13 answers
289k views

The new Java 8 stream framework and friends make for some very concise Java code, but I have come across a seemingly-simple situation that is tricky to do concisely. Consider a List<Thing> ...
Yona Appletree's user avatar
311 votes
3 answers
270k views

I've got this code: private async void ContextMenuForGroupRightTapped(object sender, RightTappedRoutedEventArgs args) { CheckBox ckbx = null; if (sender is CheckBox) { ckbx = ...
B. Clay Shannon-B. Crow Raven's user avatar
307 votes
12 answers
564k views

I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects. Ex: public class Employee { public string FirstName {set; get;} public string ...
DotnetDude's user avatar
  • 11.8k
306 votes
7 answers
210k views

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 ...
Nathan Merrill's user avatar
301 votes
4 answers
156k views

The question is confusing, but it is much more clear as described by the following code: List<List<T>> listOfList; // add three lists of List<T> to listOfList, for example /*...
David.Chu.ca's user avatar
  • 39.1k
285 votes
14 answers
379k views

I have a foreach loop reading a list of objects of one type and producing a list of objects of a different type. I was told that a lambda expression can achieve the same result. var origList = List<...
Stratton's user avatar
  • 3,603
285 votes
1 answer
16k views

I recently came across the following esoteric piece of code. int main(){(([](){})());} Reformat it as follows to make it more readable: int main(){ (([](){})()); // Um... what?!?! } But I can'...
Mysticial's user avatar
  • 473k
284 votes
13 answers
548k views

I'm using a Java lambda to sort a list. How can I sort it in a reverse way? I saw How to sort ArrayList<Long> in decreasing order?, but I want to use a Java 8 lambda. Here is my code (I used * -...
Elad Benda2's user avatar
  • 15.8k
277 votes
1 answer
225k views

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 ...
sdanna's user avatar
  • 3,109
275 votes
8 answers
460k views

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....
Vijay's user avatar
  • 3,660
270 votes
1 answer
117k views

I recently discovered that I can use lambdas to create simple event handlers. I could for example subscribe to a click event like this: button.Click += (s, e) => MessageBox.Show("Woho"); ...
Svish's user avatar
  • 159k
266 votes
7 answers
497k views

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 ...
vaibhavvc1092's user avatar
265 votes
9 answers
466k views

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 ...
user3610470's user avatar
  • 2,651

1
2 3 4 5
607