- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Expected usage for sliding?
Tue, 2010-10-19, 16:06
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
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
Tue, 2010-10-19, 16:27
#2
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.
>
Tue, 2010-10-19, 17:57
#3
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.
>>
>
>
Tue, 2010-10-19, 18:07
#4
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.
>>
>
>
Tue, 2010-10-19, 18:07
#5
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.
>>
>
>
Tue, 2010-10-19, 18:57
#6
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.
>>>
>>
>>
>
>
>
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.