Java8 stream bridge for Iterables

Issue #43 resolved
Ali Kord created an issue

Because:

public Iterable<B> getBs(Iterable<A> as, Function<A, B> f) {
        return StreamSupport.stream(as.spliterator(), false).map(f).collect(toList());
}

can be a little bit nicer, e.g.:

Iterables.stream(as).map...
...
Iterables.parallelStream(as)...

Comments (8)

  1. Ali Kord reporter

    Fair point, although Java allows it (derp):

    StreamSupport.stream(iterable.spliterator(), true)...

  2. Anund McKague

    Something like: public Stream<A> stream(Iterables<A> as)?

    Some background on why it isn't there on the base iterable interface. http://stackoverflow.com/a/23177907 Something to keep in mind from the mailing list http://mail.openjdk.java.net/pipermail/lambda-libs-spec-experts/2013-June/002031.html

    Perhaps what would be more useful is function is something equivalent to a collect on streams taking a java 8 collector https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#collect-java.util.stream.Collector- Something like public <B,C> C collect(Iterable<A> as, Collector<? super A, B, C> collector)

    Almost but not quite a fold function. (the implementation would probably forward to Function.fold)

  3. Ali Kord reporter

    I like @amckague's proposal, since we seem to have covered other Stream functionality, like map(), filter(), all(), any(), etc.

  4. Matthias Braun

    Hi and thanks for your great library!

    I'm curious whether there are now Collectors for working with eithers inside streams.

    I have written my own Collector for sequencing eithers and published it here for feedback.

    Best, Matthias

  5. Anund McKague

    @mb720 we've also taken a look at a collector based sequence. Instead of using a collector directly we did this https://bitbucket.org/atlassian/fugue/src/956cecb0af588c9bd70a42ac1645336e24b10e88/fugue/src/main/java/io/atlassian/fugue/Try.java#lines-90 for try for iterables. There's room to do something similar for streams of either/try instead of an iterable.

    The goal would be something like Streams.sequence(stream, collector) returning the container type the collector produces.

  6. Log in to comment