Collector's accumulator
Paul Sandoz
paul.sandoz at oracle.com
Thu Apr 4 01:10:33 PDT 2013
On Apr 3, 2013, at 7:36 PM, Jose <jgetino at telefonica.net> wrote:
> In the days of the earliest builds, I asked for something like this:
>
>
> polyline.nodes().cumulate((p0,p1)->new Segment(p0,p1))
>
>
> to get the segments of a polyline, out from the vertices.
>
>
> I read the docs of the b83 but is seems that the Collector accumulator
> does not support bifuncions on the form:
>
> (U,U)-->V
>
> but only mixed type arguments, (V,U)-->V.
>
U is the element type and V is the result type i.e. the accumulator accumulates results :-) it does not do anything with pairs of elements.
One trick is to change V from a Segment to something that holds them e.g. SegmentList. But it is harder for parallel evaluation to combine two SegmentList instances since you do not know the index in the stream of each vertex.
Again this is one of those examples that appears easy but there are tricky details when parallelism is considered.
Essentially you want to combine pairs of elements and accumulate those pairs into a list.
One solution is to zip up the vertices with an index to determine how to combine pairs in a SegmentList:
Streams.zip(polyline.vertices().stream(), Streams.intRange(0, polyline.vertices().size()).boxed(), (p, i) -> new ElementWithIndex(p, i)).
collect(mySegmentCollector());
Another solution is to zip the vertex stream with another copy offset by 1.
Streams.zip(polyline.vertices().stream(), polyline.vertices().stream().substream(1), (p0, p1) -> new Segment(p0, p1)).
collect(toList());
Note that the current zip implementation is not very efficient.
Paul.
> Any alternative?
>
>
>
>
>
More information about the lambda-dev
mailing list