RFR : JDK-8001642 : Add Optional<T>, OptionalDouble, OptionalInt, OptionalLong

Ali Lahijani alahijani at gmail.com
Fri Mar 15 12:53:10 PDT 2013


I guess limit() can be used to achieve short circuiting:

T findFirst(T ifNone) {
  return limit(1).reduce(ifNone, (l, r) -> r);
}

On Fri, Mar 15, 2013 at 10:26 PM, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:

> It wasn't obvious to me until recently that it is the short-circuiting
> behavior of the 'find' methods that is hardest to derive.
>
> Without short-circuiting, findFirst forms could be derived as follows, but
> they will fail on infinite streams:
>
> T findFirst(T ifNone) {
>   return reduce(ifNone, (l, r) -> (l != ifNone) ? l : r);
> }
>
> T findFirst(Predicate<? super T> predicate, T ifNone) {
>   return reduce(ifNone, (l, r) -> (l != ifNone || !predicate.test(r)) ? l :
> r);
> }
>
> I'm not sure why, but I'm liking the idea of only adding findAny(predicate,
> ifNone).
>
> --Joe
>
>
> On Fri, Mar 15, 2013 at 8:04 AM, Doug Lea <dl at cs.oswego.edu> wrote:
>
> > On 03/15/13 09:46, Brian Goetz wrote:
> >
> >> Wouldn't the minimal change NOT have a predicate, to match the existing
> >> form of
> >> findFirst?
> >>
> >>    Optional<T> findFirst()
> >>    T findFirst(T orElse)
> >>
> >>
> > Yes and no. The only way to get non-optional-bearing
> > result for search would otherwise be s.filter(pred).findAny(),
> > which entails buffering of stuff you will throw away.
> >
> > This is also the reason only adding why findAny(pred) (not findDirst)
> > is defensible: the alternative is of most interest to the sort of
> > person who want to avoid that Optional too.
> >
> > -Doug
> >
> >
> >
> >
> >
> >> On 3/15/2013 7:31 AM, Doug Lea wrote:
> >>
> >>> On 03/15/13 06:26, Joe Bowbeer wrote:
> >>>
> >>>> Doug,
> >>>>
> >>>> I think your point that Optional and non-Optional forms of reduce are
> >>>> already
> >>>> provided is significant.
> >>>>
> >>>> I noticed that your proposed versions of findFirst and findAny have a
> >>>> Predicate
> >>>> argument, but the Optional forms do not:
> >>>>
> >>>> T findFirst(Predicate<? super T> predicate, T ifNone);
> >>>>
> >>>> Why is this?
> >>>>
> >>>
> >>>
> >>> It's in the spirit of proposing a minimal change. The predicate
> >>> form suffices for all Optional-avoiding search stuff. To reduce
> >>> impact by another 50%, it would suffice to ONLY include the "any" form.
> >>>    T findAny(Predicate<? super T> predicate, T ifNone);
> >>>
> >>> -Doug
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>
> >
>


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