/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.*;
import java.util.function.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
Stream<String> fizz = pattern(3, "Fizz");
Stream<String> buzz = pattern(5, "Buzz");
Stream<String> pattern = zip((a, b) -> a + b, fizz, buzz);
Stream<String> fizzbuzz = zip((n, p) -> p.isEmpty() ? ""+n : p, nums().boxed(), pattern);
fizzbuzz.
limit(100).
forEach(System.
out::println
); }
static IntStream nums() {
return IntStream.iterate(1, i -> i + 1);
}
static Stream
<String
> pattern
(int n,
String pattern
) { return nums().mapToObj(i -> i % n == 0 ? pattern : "");
}
static <T, U, R> Stream<R> zip(BiFunction<T, U, R> zipper, Stream<T> ts, Stream<U> us) {
Iterator<T> tsItr = ts.iterator();
Iterator<U> usItr = us.iterator();
Iterator<R> iterator = new Iterator<R>() {
public boolean hasNext() {
return tsItr.hasNext() && usItr.hasNext();
}
public R next() {
return zipper.apply(tsItr.next(), usItr.next());
}
};
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLio7CmltcG9ydCBqYXZhLnV0aWwuZnVuY3Rpb24uKjsKCi8qIE5hbWUgb2YgdGhlIGNsYXNzIGhhcyB0byBiZSAiTWFpbiIgb25seSBpZiB0aGUgY2xhc3MgaXMgcHVibGljLiAqLwpjbGFzcyBJZGVvbmUKewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4gKFN0cmluZ1tdIGFyZ3MpIHRocm93cyBqYXZhLmxhbmcuRXhjZXB0aW9uCgl7CgkJU3RyZWFtPFN0cmluZz4gZml6eiA9IHBhdHRlcm4oMywgIk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