Comparator.reversed() does not compile using lambda

This is a weakness in the compiler’s type inferencing mechanism. In order to infer the type of u in the lambda, the target type for the lambda needs to be established. This is accomplished as follows. userList.sort() is expecting an argument of type Comparator<User>. In the first line, Comparator.comparing() needs to return Comparator<User>. This implies … Read more

python max function using ‘key’ and lambda expression

lambda is an anonymous function, it is equivalent to: def func(p): return p.totalScore Now max becomes: max(players, key=func) But as def statements are compound statements they can’t be used where an expression is required, that’s why sometimes lambda‘s are used. Note that lambda is equivalent to what you’d put in a return statement of a … Read more

Python lambda closure scoping

The reason is that closures (lambdas or otherwise) close over names, not values. When you define lambda x: test_fun(n, x), the n is not evaluated, because it is inside the function. It is evaluated when the function is called, at which time the value that is there is the last value from the loop. You … Read more

Using lambda expression to connect slots in pyqt

The QPushButton.clicked signal emits an argument that indicates the state of the button. When you connect to your lambda slot, the optional argument you assign idx to is being overwritten by the state of the button. Instead, make your connection as button.clicked.connect(lambda state, x=idx: self.button_pushed(x)) This way the button state is ignored and the correct … Read more

How will Java lambda functions be compiled?

The VM decides how to implement lambda, not a compiler. See Translation strategy section in Translation of Lambda Expressions. Instead of generating bytecode to create the object that implements the lambda expression (such as calling a constructor for an inner class), we describe a recipe for constructing the lambda, and delegate the actual construction to … Read more

How to force max to return ALL maximum values in a Java Stream?

I believe the OP is using a Comparator to partition the input into equivalence classes, and the desired result is a list of members of the equivalence class that is the maximum according to that Comparator. Unfortunately, using int values as a sample problem is a terrible example. All equal int values are fungible, so … Read more

How can I throw CHECKED exceptions from inside Java 8 streams?

The simple answer to your question is: You can’t, at least not directly. And it’s not your fault. Oracle messed it up. They cling on the concept of checked exceptions, but inconsistently forgot to take care of checked exceptions when designing the functional interfaces, streams, lambda etc. That’s all grist to the mill of experts … Read more

C++11 does not deduce type when std::function or lambda functions are involved

The issue is on the nature of lambdas. They are function objects with a fixed set of properties according to the standard, but they are not a function. The standard determines that lambdas can be converted into std::function<> with the exact types of arguments and, if they have no state, function pointers. But that does … Read more

Why filter() after flatMap() is “not completely” lazy in Java streams?

TL;DR, this has been addressed in JDK-8075939 and fixed in Java 10 (and backported to Java 8 in JDK-8225328). When looking into the implementation (ReferencePipeline.java) we see the method https://stackoverflow.com/questions/29229373/why-filter-after-flatmap-is-not-completely-lazy-in-java-streams @Override final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) { do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink)); } which will be invoke for findFirst operation. The special thing … Read more