Automatic replacing all anonymous inner class to lambda in Intellij Idea

In the Analyze menu, select “Run Inspection by Name…”. In the search box, type “Anonymous” and select the one that says “Anonymous class may be replaced by lambda” or something to that effect. Select your scope and start the analysis. In the results, you can inspect them individually and click the “Replace with lambda” link … Read more

Implementing an interface with two abstract methods by a lambda expression

Lambda expressions are only usable with functional interface as said by Eran but if you really need multiple methods within the interfaces, you may change the modifiers to default or static and override them within the classes that implement them if necessary. public class Test { public static void main(String[] args) { I1 i1 = … Read more

Java 8 parallel sorting vs Scala parallel sorting

One can implement a parallel sort in scala e.g. http://blog.yunglinho.com/blog/2013/03/19/parallel-external-merge-sort/ . I don’t know why ParSeq doesn’t offer sorting in its API. Note that there are a number of alternatives to .par, so don’t necessarily assume that your results from ParSeq are representative of Scala parallelism in general. (But benchmarks are valuable and I wish … Read more

Lambda passed to template not defined

It seems to me this is a compiler glitch. Using Clang compiler of Visual Studio 2017 only this error is generated “cannot refer to class template ‘foo’ without a template argument list” for a and b instantiation in main function. If the function type is specified as template parameter, there are no warnings and no … Read more

Can I use a constexpr value in a lambda without capturing it?

Should this be considered a bug in clang 3.8? Yep. A capture is only needed if [expr.prim.lambda]/12 mandates so: Note in particular the highlighted example. f(x) does not necessitate x to be captured, because it isn’t odr-used (overload resolution selects the overload with the object parameter). The same argumentation applies to your code – [basic.def.odr]/3: … Read more

IEqualityComparer for anonymous type

The trick is to create a comparer that only works on inferred types. For instance: public class Comparer<T> : IComparer<T> { private Func<T,T,int> _func; public Comparer(Func<T,T,int> func) { _func = func; } public int Compare(T x, T y ) { return _func(x,y); } } public static class Comparer { public static Comparer<T> Create<T>(Func<T,T,int> func){ return … Read more

Local reference to std::cout captured by lambda without asking for it

There’s an open clang report that cover the case of implicit capture of references by lambda expressions, this is not limited to std::cout but to references variable that are found to refer to constant expressions. For more reference, the backing defect report on the CWG is CWG-1472 EDIT: Based on @Rakete1111 comment, I should have … Read more

Lambda functions vs bind, memory! (and performance)

Closures (or arrow functions, aka lambdas) don’t cause memory leaks Can someone to confirm or infirm if the local variables (here el) can’t be cleared by the garbage collector? Or, are modern browsers capable to detect they are unused in the closure? Yes, modern JavaScript engines are able to detect variables from parent scopes that … Read more