Stream from Iterable
Stephen Colebourne
scolebourne at joda.org
Tue Dec 11 08:18:00 PST 2012
The latest code in hg has no stream() method on Iterable. That feels wrong.
I have on a number of occasions defined an API to accept Iterable when
I don't care what type of collection it is. It is more friendly to
users that way. Those APIs will have no easy way to transition to
streams (the code you pasted is not easy).
I'm assuming that the decision to not implement Streamable on Iterable
was deliberate, just saying that it degardes the usefulness of
Iterable (and its an interface that the JDK has never fully embraced).
Stephen
On 11 December 2012 16:06, Brian Goetz <brian.goetz at oracle.com> wrote:
> I do not expect this to be a common operation; clients will obtains streams
> by calling the stream() method on a Streamable. This technique is a
> lower-level API for *implementors* of Streamable. And I think you'll agree
> that this is far, far less verbose than what you have to do to implement
> Iterable...
>
> The #1 source of Iterables is Collections. All the Collections have
> stream() methods; their implementations are one-liners like this one.
>
> Actually, this is one of the weaker ways to implement a Stream; if you have
> a real data structure, you'll probably want to implement Spliterator. But
> if all you have is an Iterator, we can turn it into a stream for you.
>
>
> On 12/11/2012 10:27 AM, Stephen Colebourne wrote:
>>
>> On 11 December 2012 14:29, Brian Goetz <brian.goetz at oracle.com> wrote:
>>>
>>> Try this:
>>>
>>> Streams.stream(Streams.spliterator(iter.iterator(), sizeIfKnown),
>>> flags);
>>>
>>> You might want ORDERED as a flag.
>>
>>
>> That seems quite verbose for what seems like a common operation...
>>
>> Stephen
>>
>
More information about the lambda-dev
mailing list