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

前端 未结 3 681
萌比男神i
萌比男神i 2020-12-20 12:08

According to scaladoc, sliding() returns... \"An iterator producing iterable collections of size size, except the last and the only element will be truncated if

相关标签:
3条回答
  • 2020-12-20 12:29

    It was a mistake, but wasn't fixed as of 2.9. Everyone occasionally makes design errors, and once one gets into the library it's a nontrivial task to remove it.

    Workaround: add a filter.

    xs.sliding(3).filter(_.size==3).toList
    
    0 讨论(0)
  • 2020-12-20 12:49

    You can "work around" this by using the GroupedIterator#withPartial modifier.

    scala> val xs = List(1, 2)
    xs: List[Int] = List(1, 2)
    
    scala> xs.iterator.sliding(3).withPartial(false).toList
    res7: List[Seq[Int]] = List()
    

    (I don't know why you need to say xs.iterator but xs.sliding(3).withPartial(false) does not work because you get an Iterator instead of a GroupedIterator.

    0 讨论(0)
  • 2020-12-20 12:49

    EDIT:

    Check Rex's answer (which is the correct one). I'm leaving this just because (as Rex said on the comments) it was the original (wrong) idea behind that design decision.


    I don't know why you would expect an empty list there, returning the full list seems like the best result, consider this example:

    def slidingWindowsThing(windows : List[List[Int]]) { // do your thing
    

    For this methods you probably want all these calls to work:

    slidingWindowsThing((1 to 10).sliding(3))
    
    slidingWindowsThing((1 to 3).sliding(3))
    
    slidingWindowsThing((1 to 1).sliding(3))
    

    This is why the method defaults to a list of size list.length instead of Nil (empty list).

    0 讨论(0)
提交回复
热议问题