Hacker News new | ask | show | jobs
by sgdread 3859 days 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);
    }