Skip to content

Tag: functional-programming

how to fix the second parameter of a function with vavr?

Suppose I have a function which takes two parameter. I want to fix the second parameter and makes it a Function1<T1,R>. With Function2.apply(T1 t), I can only fix the first parameter, is there a way to fix the second parameter? Answer There’s no utility function built into vavr that does a partial application of the second argument. The available utility

How to implement the Elvis operator in Java 8?

I have the classic “Elvis operator” case, where I’m calling methods that each may return null and chaining them together: In Java 8, the most faithful implementation I’ve found is something like this: I wish that Java’s Optional had something akin to the elvis operator: So that I wouldn’t have to wrap each return value: Is there a more efficient

Count the same items in a row in Java 8 Stream API

I have a bean and a stream and want to apply some intermediate operation to the stream that returns another stream of objects of TokenBag. In this example there must be two: (“a”, 3), (“b”, 3) and (“a”, 2). Please think it as a very simplistic example. In real there will be much more complicated logic than just counting the

Java strange class cast exception

Could anyone help me understand why in 1 situation I do not have ClassCastException? At least String::trim is not a MagicFunction. Answer So, method references (like String::trim) are a bit weird; unlike most expressions in Java, they don’t really have a type of their own. Something like this: won’t even compile, because it doesn’t give the compiler enough information about