Grouping stream elements by their position - how to handle tail of stream ?
Lattie
latsama at gmail.com
Sat Feb 2 12:59:28 PST 2013
Boaz,
I just wanted to validate your request as I ran into *exactly* the
same issue yesterday.
I think that explode ought to call some sort of finalizer method at
end of stream to accommodate those BiBlocks which are stateful.
Also, I'm not sure if 'explode' is the best name as I find I am using
it more generally to transform a stream of X into a stream of Y, where
Y may require 1 or more X's as inputs. (I didn't find any other
method that enabled that type of transformation. Please feel free to
educate me :)
On Sat, Feb 2, 2013 at 12:35 PM, Boaz Nahum <boaznahum at gmail.com> wrote:
> Hi.
>
> I looking in Stream interface for a way to convert a stream of
> { t1, t2, .. Tn }
> to
> { {t1,t2}, ... {Tn-1, Tn} }
> or
> { {t1,t2}, ... {Tn, null}}
>
> Lets assume {t1, t2} are aggeraget by Pair<T,T>
>
>
>
> So I tried to use explode:
>
> * Stream<Integer> si = Arrays.asList(1, 2, 3, 4, 5).stream();
>
> Stream<Pair<Integer, Integer>> spi = si.sequential().explode(new
> BiConsumer<Stream.Downstream<Pair<Integer, Integer>>, Integer>() {
>
> Integer firstValue;
>
> @Override
> public void accept(Stream.Downstream<Pair<Integer, Integer>>
> pairDownstream, Integer v) {
>
> if (firstValue == null) {
> firstValue = v;
> } else {
>
> pairDownstream.send(new Pair<>(firstValue, v));
> firstValue = null;
>
> }
> }
>
> });
> *
>
> But I didn't find a way to add the tail of input stream {.., 5} to the new
> stream { ,,, {5, null}}.
>
> Of-course this only simplified example of more general problem I have.
>
> Thanks
> Boaz
>
More information about the lambda-dev
mailing list