Stream of a reverse list
Dan Smith
daniel.smith at oracle.com
Tue Jul 16 12:59:14 PDT 2013
On Jul 15, 2013, at 1:01 PM, David M. Lloyd <david.lloyd at redhat.com> wrote:
> On 07/15/2013 01:19 PM, Dan Smith wrote:
>> On Jul 15, 2013, at 7:53 AM, Remi Forax <forax at univ-mlv.fr> wrote:
>>
>>> On 07/15/2013 03:36 PM, Brian Goetz wrote:
>>>> We did consider such a stream op and triaged it away as bring too niche. It also requires a full barrier to get the first element. And for infinite streams obviously blows up.
>>>>
>>>> Given that it always requires a full barrier, toArray seems the best way to go.
>>>>
>>>> Sent from my iPad
>>>
>>> We reject having a method reverse() on Stream but not the fact that one can create a Stream that will iterate over the list in a backward way.
>>> But perhaps, it's better to have a method of List named reverseList() that return a reverse view of the list
>>> and calls stream() on it.
>>
>> Has to be prioritized, of course, but I think 'List.reverseStream' is in principle a good suggestion. Looks a lot like the idea of having different methods on CharSequence to get char-based and int-based views of the same data. In this case, we're getting front-to-back and back-to-front views of the List.
>>
>> Collection is _not_ a good place to put a method like this, because Collections are not designed to support reverse-order traversal. Lists are (see ListIterator). As are Deques (see Deque.desendingIterator). (I was surprised, actually, to not find a similar List.reverseIterator method -- I guess the intended idiom is to call 'list.iterator(list.size())' and then iterate with 'ListIterator.previous'.)
>
> If that is directed at me... I did not suggest Collection, I suggested Collections, in lieu of adding a default method to List (though that's an option too).
Sort of a conglomeration of you mentioning Collections and Brian talking about a Stream method. But, yeah, I did realize when reading carefully that you were talking about a static method operating on Lists.
The idea is it's fairly painless to "streamify" existing concepts, like reverse List and Deque iteration; much more expensive (and perhaps ill-advised) to create new concepts, like every Stream having a reverse order.
—Dan
More information about the lambda-libs-spec-observers
mailing list