Spliterator.tryAdvance

Remi Forax forax at univ-mlv.fr
Sun Feb 10 09:25:20 PST 2013


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.

I would like to have tryAdvance to be like this:

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

    /**
      * If a remaining element exists, performs the given action on it,
      * returning the result of the function, otherwise returns {@code END}.
      *
      * @param action The action to perform.
      * @return {@code END} if no remaining elements existed
      * upon entry to this method, else the return value of the action.
      */
    Object tryAdvance(Function<? super T, ?> action);

and forEach is a little bit uglier:
     Function<? super T, ?> action = element -> { 
consumer.accept(element); return null; }
     do {} while(tryAdvance(action) != END);

with that, there is no need to use a side value to express the fact that 
we have already found the resulting value because we can return it has 
the return value of tryAdvance.

cheers,
Rémi



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