This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Expected usage for sliding?

6 replies
ichoran
Joined: 2009-08-14,
User offline. Last seen 2 years 3 weeks ago.
sliding(n) returns views of n-at-a-time of an underlying sequence.

What is the expectation for sliding(n) for a sequence of less than length n?

Right now, if the entire sequence has 1..n-1 elements, you get one view of the entire sequence.  I was intuitively expecting that if you can't get all n, you wouldn't get any.  So

  List(1).sliding(2).toList

would return

  List[List[Int]] = List()

not

  List[List[Int]] = List(List(1))

as it does now.  The former is nice because it automatically ignores corner cases, rather than requiring you to handle corner cases of "I asked for n but didn't get it".

One can make it work the way I expected trivially enough with .sliding(n).filter(_.length==n), but I wonder if others who have used this have been surprised by the behavior, and if this surprise is enough to change it.  (Probably not, at this point, since someone has surely relied upon the existing behavior, but I figure it's worth asking.)

  --Rex

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Expected usage for sliding?

On Tue, Oct 19, 2010 at 11:05:56AM -0400, Rex Kerr wrote:
> What is the expectation for sliding(n) for a sequence of less than
> length n?

It does the wrong thing. Here's a comment I have on some work in
progress.

+ // XXX perhaps sliding should be changed not to ever return too small

You can scratch "perhaps". The reason it is the way it is now is that
the invariant I chose when I wrote it is that it would always deliver
all the elements. Now having used it in anger, the more important
invariant is that every group be the same size.

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Expected usage for sliding?

+1

Am 19.10.2010 17:14, schrieb Paul Phillips:
> On Tue, Oct 19, 2010 at 11:05:56AM -0400, Rex Kerr wrote:
>> What is the expectation for sliding(n) for a sequence of less than
>> length n?
> It does the wrong thing. Here's a comment I have on some work in
> progress.
>
> + // XXX perhaps sliding should be changed not to ever return too small
>
> You can scratch "perhaps". The reason it is the way it is now is that
> the invariant I chose when I wrote it is that it would always deliver
> all the elements. Now having used it in anger, the more important
> invariant is that every group be the same size.
>

pkolaczk
Joined: 2010-01-14,
User offline. Last seen 2 years 38 weeks ago.
Re: Expected usage for sliding?

And another +1 for this.
I've already run into that some time ago.

W dniu 2010-10-19 17:22, HamsterofDeath pisze:
> +1
>
> Am 19.10.2010 17:14, schrieb Paul Phillips:
>> On Tue, Oct 19, 2010 at 11:05:56AM -0400, Rex Kerr wrote:
>>> What is the expectation for sliding(n) for a sequence of less than
>>> length n?
>> It does the wrong thing. Here's a comment I have on some work in
>> progress.
>>
>> + // XXX perhaps sliding should be changed not to ever return too small
>>
>> You can scratch "perhaps". The reason it is the way it is now is that
>> the invariant I chose when I wrote it is that it would always deliver
>> all the elements. Now having used it in anger, the more important
>> invariant is that every group be the same size.
>>
>
>

pkolaczk
Joined: 2010-01-14,
User offline. Last seen 2 years 38 weeks ago.
Re: Expected usage for sliding?

And another +1 for this.
I already ran into that some time ago.

W dniu 2010-10-19 17:22, HamsterofDeath pisze:
> +1
>
> Am 19.10.2010 17:14, schrieb Paul Phillips:
>> On Tue, Oct 19, 2010 at 11:05:56AM -0400, Rex Kerr wrote:
>>> What is the expectation for sliding(n) for a sequence of less than
>>> length n?
>> It does the wrong thing. Here's a comment I have on some work in
>> progress.
>>
>> + // XXX perhaps sliding should be changed not to ever return too small
>>
>> You can scratch "perhaps". The reason it is the way it is now is that
>> the invariant I chose when I wrote it is that it would always deliver
>> all the elements. Now having used it in anger, the more important
>> invariant is that every group be the same size.
>>
>
>

pkolaczk
Joined: 2010-01-14,
User offline. Last seen 2 years 38 weeks ago.
Re: Expected usage for sliding?

And another +1 for this.
I ran into that some time ago and was really surprised.

W dniu 2010-10-19 17:22, HamsterofDeath pisze:
> +1
>
> Am 19.10.2010 17:14, schrieb Paul Phillips:
>> On Tue, Oct 19, 2010 at 11:05:56AM -0400, Rex Kerr wrote:
>>> What is the expectation for sliding(n) for a sequence of less than
>>> length n?
>> It does the wrong thing. Here's a comment I have on some work in
>> progress.
>>
>> + // XXX perhaps sliding should be changed not to ever return too small
>>
>> You can scratch "perhaps". The reason it is the way it is now is that
>> the invariant I chose when I wrote it is that it would always deliver
>> all the elements. Now having used it in anger, the more important
>> invariant is that every group be the same size.
>>
>
>

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: Re: Expected usage for sliding?

3 times? really evil bug :)

Am 19.10.2010 18:54, schrieb Piotr Kołaczkowski:
> And another +1 for this.
> I ran into that some time ago and was really surprised.
>
> W dniu 2010-10-19 17:22, HamsterofDeath pisze:
>> +1
>>
>> Am 19.10.2010 17:14, schrieb Paul Phillips:
>>> On Tue, Oct 19, 2010 at 11:05:56AM -0400, Rex Kerr wrote:
>>>> What is the expectation for sliding(n) for a sequence of less than
>>>> length n?
>>> It does the wrong thing. Here's a comment I have on some work in
>>> progress.
>>>
>>> + // XXX perhaps sliding should be changed not to ever return
>>> too small
>>>
>>> You can scratch "perhaps". The reason it is the way it is now is that
>>> the invariant I chose when I wrote it is that it would always deliver
>>> all the elements. Now having used it in anger, the more important
>>> invariant is that every group be the same size.
>>>
>>
>>
>
>
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland