Let's say I have this imperative code:
List<Function<T, T>> functions = ...
T value = ...
for (Function<T, T> function : functions) {
value = function.apply(value);
}
How do I write this in the functional style (like what fold does in Scala)?
You can use the flatMap function to convert a list of lists to a big list containing all elements from individual lists. 5. Similar to the map function, flatMap is also an intermediate operation which means you can call other stream methods after calling this one.
The “toString()” method can be used to print a list in Java. This method prints the specified list elements directly using their references.
This has just been asked a few hours ago for a Consumer
... You could reduce them to a single function and apply that:
@SafeVarargs
private static <T> Function<T, T> combineF(Function<T, T>... funcs) {
return Arrays.stream(funcs).reduce(Function.identity(), Function::andThen);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With