map and reduce functions on list

Brian Goetz brian.goetz at oracle.com
Mon Dec 10 09:45:48 PST 2012


Yes, this is a common question.  The short answer is no.

The reason is: Iterable has strong associations with "something that can 
be iterated multiple times" (even though the spec doesn't come right out 
and say that.)  Streams are more like IteratOR than IteraBLE.  But some 
stream sources might be fed by IO, and would not be Iterable.  Having 
the stream methods on Iterable led to multiple forms of confusion.  (We 
got a lot of confused queries along the line of "how do I know whether 
the collection is in eager or lazy mode?"  Obviously this doesn't make 
sense, but if this is what stuffing the methods into Iterable makes 
people think, that's a bad sign.)

Further, if the methods were on Iterable, then Collection would have a 
mix of eager and lazy methods with similar names, and the net result 
would be very confusing.  For example, filter(Predicate) is lazy and 
creates a new Stream, but removeAll(Predicate) mutates the collection 
in-place.  A user hitting ctrl-space would then have to reason about 
which are the "mutative old" method and which are the "functional new" 
ones.  Not what we were trying to do.

The syntactic overhead of the stream() call is regrettable -- it took us 
a long time to get over it too -- but once you get past the distaste, 
the resulting model is dramatically cleaner.

On 12/10/2012 12:04 PM, Venkat Subramaniam wrote:
> Greetings,
>
> Currently playing with b67 build and having a great time with it.
>
> To use the map and reduce functions on a list, I'm calling the stream() method first
> on a list. It would be convenient to have these methods directly on an Iterable (like
> it was in a version a few months ago). Are there plans to bring these methods
> back on Iterable or is doing the conversion to Stream the path forward.
>
> My sincere apologies if this was discussed earlier and I totally missed it (a quick scan on
> the list did not help me find the discussion). Much appreciate the response.
>
> Thank you
>
> Venkat
>


More information about the lambda-dev mailing list