To Stream.slice(fromInclusive, toExclusive) or Stream.slice(toSkip, limit) that is the question

Stephen Colebourne scolebourne at joda.org
Fri Oct 11 03:32:00 PDT 2013


skip() and limit() make perfect sense. I'm surprised by the notion
that skip(60).limit(10) would be less efficient than
skipAndLimit(60,10) - that seems like an implementation detail that
should not drive the API. Thus I dislike the current slice definition.

More generally, my expectation from experience is that slice would be
slice(start, endExclusive). I'd also expect slice to support negative
indicies to refer releative to the end, otherwise I'd be wondering why
it wasn't called subStream.

Thus my preference is:
- skip(elementsToSkip)
- limit(elementLimit)
- subStream(startInclusive, endExclusive)
- make skip().limit() efficient internally

I don't think anyone will be surprised by those three methods.

Stephen


On 11 October 2013 10:52, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:
> slice notation in groovy is also start..end, and slice notation in scala
> and clojure is start:end.
>
> Those are all the languages I know that have a slice. In what language is a
> slice parameterized by start:count?
> On Oct 11, 2013 1:12 AM, "Paul Sandoz" <paul.sandoz at oracle.com> wrote:
>
>> Hi Joe,
>>
>> I tend to think of slice(s, l) as the fused (optimal) form of
>> skip(s).limit(l). For parallel streams the the fused form will result in
>> less wrapping and/or buffering (depending on the properties of the input
>> stream). Documentation-wise we should probably include an api note on skip
>> and limit referring to slice in this respect.
>>
>> Paul.
>>
>> On Oct 11, 2013, at 3:28 AM, Joe Bowbeer <joe.bowbeer at gmail.com> wrote:
>>
>> > slice(start, end) would be more useful and more consistent with its use
>> in
>> > other languages (Python, Perl).
>> >
>> > In Python, the elements are start .. end-1 whereas the end is inclusive
>> in
>> > Perl.  But I think the use of start:end is fairly consistent for most
>> > implementations of slice.
>> >
>> > I claim this would be more useful because otherwise there's no difference
>> > between slice and skip(start) + limit(end-start)
>> >
>> > --Joe
>> >
>> >
>> > On Thu, Oct 10, 2013 at 5:04 PM, Brian Goetz <brian.goetz at oracle.com>
>> wrote:
>> >
>> >> FWIW, this is what the semantics were originally, and they got modified
>> >> to be consistent with substring() when we renamed slice to substring()
>> >> originally.  So this is a reversion to where we were before.
>> >>
>> >>
>> >> On 10/10/2013 7:54 PM, Mike Duigou wrote:
>> >>
>> >>> Hello all;
>> >>>
>> >>> In the review of the renaming patch for Stream.substream() -> slice()
>> >>> Brian asked me to consider also changing the semantics of the
>> >>> parameters from the current Stream.slice(fromInclusive,**toExclusive).
>> >>> The rationale is that we then have only one sense of usage in the
>> >>> parameters for skip/limit/slice. This also makes slice() more
>> >>> obviously equivalent to skip(toSkip).limit(limit). I am inclined to
>> >>> agree with him that using the same semantics for the parameters
>> >>> across the three methods has value.
>> >>>
>> >>> I will go forward with changing to Stream.slice(toSkip,limit) before
>> >>> Monday assuming there is no outcry.
>> >>>
>> >>> Mike
>> >>>
>> >>>
>>
>>


More information about the lambda-libs-spec-observers mailing list