I'm student and I learning functional Java 8. I got project to do and I don't understand how this function interface work. My teacher told me "you should know that" and I'm looking for help to understand this problem. It should count Fibonacci series
I got this code
StreamUtils.generateRest(Stream.of(1, 1), (a, b) -> a + b)
.limit(7)
.forEach(System.out::println);
StreamUtils.generateRest(Stream.of("AAA", "BB", "KKKK"), (a, b) -> a + b)
.limit(7)
.forEach(System.out::println);
StreamUtils.generateRest(Stream.of(i -> 0),
(BinaryOperator<UnaryOperator<Integer>>) (f, g) -> (x -> x == 0 ? 1 : x * g.apply(x - 1)))
.limit(10)
.map(f -> f.apply(7))
.forEach(System.out::println);
I did something like this but it doesn't work
public class StreamUtils<T> {
public static <T> Stream generateRest(Stream<T> stream, BinaryOperator<T> binaryOperator) {
return Stream.of(stream.reduce((a, b) -> binaryOperator.apply(a, b)));
}
}
Someone can help me with that and explain how to solve this problem?