FlatMapper

Zhong Yu zhong.j.yu at gmail.com
Tue Feb 12 11:19:18 PST 2013


One common use case is to map zero or more elements in stream A into
one element in stream B. People can, and will, use flatMap(FlatMapper)
to achieve that (with a side effecting mapper), even though it is the
opposite of what "flat map" was known for. I think the method could
use a more general name which is appropriate for both explode/implode.

Another use case is to aggregate *some* (not all) elements of a stream
to produce a result; it can be done by
stream.flatMap(FlatMapper).findFirst(). If this use case is common
enough it deserves a standalone method, say
aggregatePartially(FlatMapper). Now if FlatMapper is needed in other
places too, it could use a more general name; after all, it is pretty
much a normal function, except it inserts its result in a sink instead
of returning it.

Zhong Yu

On Tue, Feb 12, 2013 at 12:41 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> Here's where things have currently landed with FlatMapper -- this is a type
> in java.util.stream, with nested specializations.
>
> Full bikeshed season is now open.  Are we OK with the name explodeInto()?
> Is this general enough to join the ranks of Function and Supplier as
> top-level types in java.util.function?
>
> @FunctionalInterface
> public interface FlatMapper<T, U> {
>     void explodeInto(T element, Consumer<U> sink);
>
>     @FunctionalInterface
>     interface ToInt<T> {
>         void explodeInto(T element, IntConsumer sink);
>     }
>
>     @FunctionalInterface
>     interface ToLong<T> {
>         void explodeInto(T element, LongConsumer sink);
>     }
>
>     @FunctionalInterface
>     interface ToDouble<T> {
>         void explodeInto(T element, DoubleConsumer sink);
>     }
>
>     @FunctionalInterface
>     interface OfIntToInt {
>         void explodeInto(int element, IntConsumer sink);
>     }
>
>     @FunctionalInterface
>     interface OfLongToLong {
>         void explodeInto(long element, LongConsumer sink);
>     }
>
>     @FunctionalInterface
>     interface OfDoubleToDouble {
>         void explodeInto(double element, DoubleConsumer sink);
>     }
> }


More information about the lambda-libs-spec-observers mailing list