0

Possible Duplicate:
Inconsistent behaviour for xs.sliding(n) if n is less than size?

EDIT: I realise that the behaviour described here is the expected behaviour of sliding; I'm questioning why this is the case.

EDIT 2: Duplicate of this question

I'm using the sliding function on a scala collection, and I'm surprised by the behaviour in the following case:

m.sliding( N )

where M is m.size, and M < N.

In this case, you don't get back an empty iterator, you get back a single-element iterator, where the only element is the input:

scala> List( ).sliding( 2 ).size
res0: Int = 0
scala> List( 1 ).sliding( 2 ).size
res1: Int = 1

As an example, here's a simple function to recursively calculate a row from Pascal's triangle:

def row( num : Int ) : List[ Int ] =
{
    num match
    {
        case 0 => List( 1 )
        case x : Int => List( 1 ) ++ row( x - 1 ).sliding( 2 ).map( _.sum ) :+ 1
    }
}

but this doesn't as expected, because of the behaviour discussed above:

scala> row( 1 )
res0: List[Int] = List(1, 1, 1)

Instead, we have to include an extra special case:

def row2( num : Int ) : List[ Int ] =
{
    num match
    {
        case 0 => List( 1 )
        case 1 => List( 1, 1 )
        case x : Int => List( 1 ) ++ row2( x - 1 ).sliding( 2 ).map( _.sum ) :+ 1
    }
}

and this now works as expected.

Does anyone know why sliding behaves like this? It seems strange to me that I ask for sub-collections of a certain length, and in the case discussed here you get a single collection of a different length!

Community
  • 1
  • 1
paulmdavies
  • 1,248
  • 3
  • 12
  • 28
  • What behavior are you expecting? There isn't a 'sub-collection of a certain length' to return and not returning anything would discard data... so what would you want the response to be? – The Archetypal Paul Dec 17 '12 at 11:15
  • 1
    I think I'd expect it to discard the data - it seems to me to be the useful thing to do in this case? – paulmdavies Dec 17 '12 at 11:27
  • What do you mean by 'discarding the data'? – Vincenzo Maggio Dec 17 '12 at 12:44
  • 1
    From the scaladoc: "Sliding returns: An iterator producing lists of size size, except the last and the only element will be truncated if there are fewer elements than size." It seems to me that this behavior that you think is strange is the expected one. At least to me, I would think that it would work just like it did with you. And it does. Cheers! – wleao Dec 17 '12 at 13:19
  • I'd expect it to return an empty collection. – paulmdavies Dec 17 '12 at 13:20
  • Thanks, I acknowledge that the behaviour is as expected, but I'm questioning why that's the expected behaviour - I think returning empty collection when the input size is less than the window size would be more consistent. – paulmdavies Dec 17 '12 at 13:22
  • Imagine that your list is a buffer and that you're using sliding to consume what is inside of it. If your window is greater than the current content of the buffer you would expect it to return an empty list, right? But what happens with the data? To you, having less than the window size is like having nothing. That's not correct. It's better to treat them differently than to ignore them. My example is a case of producer/consumer. But I believe it fits this scenario. Cheers! – wleao Dec 17 '12 at 13:28
  • Furthermore, try to work with the step parameter and you see that behavior more clearly. If you think that my comment answers your question, tell me and I'll be more than happy to add it as an answer. Cheers! – wleao Dec 17 '12 at 13:30
  • I've just found a [similar question](http://stackoverflow.com/questions/7958712/inconsistent-behaviour-for-xs-slidingn-if-n-is-less-than-size), which StackOverflow's "similar question finder" didn't find when I was posting this one. Apparently, the behaviour should have been what I was expecting, but it's a bug which hasn't yet been fixed. – paulmdavies Dec 17 '12 at 13:36
  • Hmm. I didn't know about that. I'm checking that out. I still think that it should return something rather than an empty list. – wleao Dec 17 '12 at 13:54
  • Yeap, now I get it. Sliding is not meant to be used while consuming something, but to return lists of size size. The problem is that a lot of people now work that way (the "wrong" way). What happens if they "fix" the way sliding works? Cheers. – wleao Dec 17 '12 at 13:57

0 Answers0