Primitive streams and optional
Doug Lea
dl at cs.oswego.edu
Sun Nov 25 05:14:33 PST 2012
... returning to the question at hand ...
On 11/21/12 12:59, Brian Goetz wrote:
> OK, I think we have all the options and combinations of options on the table.
>
> A: OptionalInt min()
> B: int min(int defaultValue)
> C: int min() throws NSEE
>
I vote for renaming to make clearer that this is a reduction with
a basis, and then providing two forms:
int least(int ceiling); // return least value < ceiling, or ceiling if none
int least(); // default ceiling == Integer.MAX_VALUE
A similar pattern of using a recursion-ish basis value is
available for all the other methods. And in many of them,
you can provide a default relying on the natural identity
element (as the minima reductions here).
The notably odd-looking case of this is findAny, for
which there is no plausible default basis. So either
don't define the basis-less overload version, or default
to exception:
int findAny(Pred pred, int basis);
int findAny(Pred pred) throws NoSuchElementException
(These are not new problems. All primitive collection
frameworks out there (like Trove) hit this issue
(for example for map.get(key)) and users seem to get
used to it. Jarringness of a few constructions seems
much better than the alternatives.)
-Doug
More information about the lambda-libs-spec-observers
mailing list