Spliterator.tryAdvance

Remi Forax forax at univ-mlv.fr
Sun Feb 10 14:19:31 PST 2013


On 02/10/2013 10:00 PM, Doug Lea wrote:
> On 02/10/13 12:25, Remi Forax wrote:
>> Playing a little bit with how findFirst/forEachUntil can be 
>> implemented on top
>> of a Spliterator,
>> I think that tryAdvance should be changed to be able to return a 
>> value produced
>> in the middle of the consumer taken by tryAdvance.
>
> Brian and I spent a while on this theme, of only supporting
> forEach and some variant of the search method I mentioned.
> If we had nonnull-element guarantees, it would be an easier call:
> just use CHM-like search. The primitive int/long/double
> versions would  need a boxed return value but these could
> sometimes be optimized away in practice. All in all seems
> pretty good. But when you also allow nullable elements,
> it means that every call is guaranteed to create a nuisance
> object, which makes it less attractive than single-step
> tryAdvance as the basic workhorse underlying a lot of bulk
> computations.

You get it wrong, I think.
What I propose is that tryAdvance is not a search like operation but a 
single-step operation.
And when it calls the action at the end, the action is able to 
back-propagate a resulting value.
You can see it has a way to abstract the way a read on an input works, 
either you get the the number of bytes read or you get -1.
Here, with tryAdvance, either you get the return value of the action or 
you get END,
you still have to call tryAdvance several times to consume the whole stream.

About null, given that the return value can only comes from the 
consumer, the return value can be null or not depending on what the user 
has specified as action.

>
>>    /**
>>     * Sentinel value used by tryAdvance to signal that there is no 
>> more element.
>>     */
>>    public static final Object END = new Object();
>
> No can do. (Primitives.)

If the primitive value is one which is used in a reduce, yes, it's true, 
it can not do that, but anyway, you can not send the reduced value to 
the action too, or you need to create a new action at each call.
Otherwise, the stream API uses Optional as box, so the stream API 
already requires the implementation to box the value.

>
> -Doug
>
>

Rémi



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