3 ms·
To close standard libs gap, let's assume I have static functions for odd() and take() as well as Java8 stream APIs statically imported. After this, it looks pre
by sgdread 11y ago
To close standard libs gap, let's assume I have static functions for odd() and take() as well as Java8 stream APIs statically imported. After this, it looks pretty compact and readable:
take(10, i -> range(0, i)
.mapToObj(j -> odd(i))
.collect(joining(" ")))
.collect(joining(" "));
For reference, odd and take will look like this:
private static String odd(int i) {
return Boolean.toString(i % 2 != 0);
}
private static <T> Stream<T> take(int n, Function<Integer, T> f) {
return Stream.iterate(1, i -> ++i).map(f).limit(n);
}