No-reuse-streams
Remi Forax
forax at univ-mlv.fr
Mon Nov 26 09:39:38 PST 2012
On 11/26/2012 06:22 PM, Paul Sandoz wrote:
> On Nov 26, 2012, at 5:45 PM, Henry Jen <henry.jen at oracle.com> wrote:
>
>> On Nov 26, 2012, at 8:21 AM, "David M. Lloyd" <david.lloyd at redhat.com> wrote:
>>
>>>> Should the following throw an ISE on the last line of the following?
>>>>
>>>> Stream s = ..
>>>> Object i1 = s.findFirst();
>>>> Stream s1 = s.map(...);
>>>>
>>>> i.e. should we fail on the s.map(...) or just when a terminal operation occurs, if at all?
>>> I agree with you and Rémi - this should fail.
>>>
>>
>>
>> While it may be "consistent", what is the obvious alternative when need to do something like this?
>> A use case like this is probably following,
>>
>> For a input stream, find a marker, and then continue to process the rest of stream.
>>
> That seems like a use-case for dropWhile or skipWhile.
take the stream, create an iterator, iterate until you have your marker,
wrap the iterator as a new stream (this trick is from Brian).
You lost some information about the size of the stream etc,
but usually you don't care because on a IO stream these informations are
not known.
>
>
>> I guess the general question is: how does one control the flow of the stream? How do we continue the stream after a terminal op which does not consume whole stream.
>>
> It's a bit funky but one could do:
>
> Stream s = ...
> Iterator i = s.iterator();
> Object first = i.next();
> i.stream()....; // Not yet supported but IIRC we have talked about making Iterator streamable.
>
> If the source is an iterator of some sorts i think detached streams might be useful for repeated processing of such a source.
>
> Note that for a parallel stream findFirst and findAny (or anyMatch) may consume more elements than the equivalent sequential stream.
>
> Paul.
see above.
Rémi
More information about the lambda-libs-spec-observers
mailing list