Difference between method reference Bound Receiver and Unbound Receiver

The idea of the unbound receiver such as String::length is you’re referring to a method of an object that will be supplied as one of the lambda’s parameters. For example, the lambda expression (String s) -> s.toUpperCase() can be rewritten as String::toUpperCase. But bounded refers to a situation when you’re calling a method in a … Read more

Lambdas and std::function

Stephan T. Lavavej explains why this doesn’t work in this video. Basically, the problem is that the compiler tries to deduce BaseT from both the std::vector and the std::function parameter. A lambda in C++ is not of type std::function, it’s an unnamed, unique non-union type that is convertible to a function pointer if it doesn’t … Read more

Declaring Func dynamically

You can do this by using an open generic type definition, and then making the specific type from that: typeof(Func<,>).MakeGenericType(typeof(int), orderType); However, what you’re trying to do (calling Lambda<TDelegate>) is not directly possible. You must call Lambda without a type parameter: var propertyinfo = typeof(T).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; var param = Expression.Parameter(typeof(T), “x”); var … Read more

Java 8 lambda for selecting top salary employee for each department

You can do that with a grouping collector: Map<String, Employee> topEmployees = allEmployees.stream() .collect(groupingBy( e -> e.department, collectingAndThen(maxBy(comparingInt(e -> e.salary)), Optional::get) )); with the static imports import static java.util.Comparator.comparingInt; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.maxBy; This code creates a Stream of all the employees and groups them with their department with the … Read more

Linq query or Lambda expression?

Query Expression compiles into Method Expression (Lambda expression), so there shouldn’t be any difference, In your code though you are accessing First and FirstOrDefault which would behave differently. See: Query Syntax and Method Syntax in LINQ (C#) and LINQ Query Expressions (C# Programming Guide) At compile time, query expressions are converted to Standard Query Operator … Read more

Why does a Java Lambda which throws a Runtime Exception require brackets?

The Java Language Specification describes the body of a lambda expression A lambda body is either a single expression or a block (ยง14.2). This, however, throw new IllegalArgumentException(“fail”) is the throw statement, not an expression. The compiler therefore rejects it as the lambda expression’s body. You can go down the rabbit hole and learn what … Read more