Using return inside a lambda?

Just use the qualified return syntax: return@fetchUpcomingTrips. In Kotlin, return inside a lambda means return from the innermost nesting fun (ignoring lambdas), and it is not allowed in lambdas that are not inlined. The return@label syntax is used to specify the scope to return from. You can use the name of the function the lambda … Read more

Using Include in Entity Framework 4 with lambda expressions

The RTM version of Entity Framework 4.1 actually includes extension methods in the EntityFramework.dll file, for eager loading with lambda through the Include function. Just include the DLL in your project and you should be able to write code like: var princesses1 = context.Princesses.Include(p => p.Unicorns).ToList(); Remember to add an Import/Using statement to include the … Read more

Java 8 Filter Array Using Lambda

Yes, you can do this by creating a DoubleStream from the array, filtering out the negatives, and converting the stream back to an array. Here is an example: double[] d = {8, 7, -6, 5, -4}; d = Arrays.stream(d).filter(x -> x > 0).toArray(); //d => [8, 7, 5] If you want to filter a reference … Read more

Proper usage of Optional.ifPresent()

Optional<User>.ifPresent() takes a Consumer<? super User> as argument. You’re passing it an expression whose type is void. So that doesn’t compile. A Consumer is intended to be implemented as a lambda expression: Optional<User> user = … user.ifPresent(theUser -> doSomethingWithUser(theUser)); Or even simpler, using a method reference: Optional<User> user = … user.ifPresent(this::doSomethingWithUser); This is basically the … Read more

How to debug stream().map(…) with lambda expressions?

I usually have no problem debugging lambda expressions while using Eclipse or IntelliJ IDEA. Just set a breakpoint and be sure not to inspect the whole lambda expression (inspect only the lambda body). Another approach is to use peek to inspect the elements of the stream: List<Integer> naturals = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13); naturals.stream() .map(n -> n * … Read more