分享 未结
0 2 0 42
小编 2017-08-15发布
收藏 点赞

sliding方法定义在IterableLike Trait中,用于将列表分割成固定大小的子列表,该方法的API文档如下:

/** Groups elements in fixed size blocks by passing a "sliding window"
 *  over them (as opposed to partitioning them, as is done in grouped.)
 *  @see [[scala.collection.Iterator]], method `sliding`
 *  @param size the number of elements per group
 *  @param step the distance between the first elements of successive
 *         groups
 *  @return An iterator producing ${coll}s of size `size`, except the
 *          last and the only element will be truncated if there are
 *          fewer elements than size.
def sliding(size: Int, step: Int): Iterator[Repr]


val list = ('a' to 'e').map(_.toString)
list.sliding(2).toList //输出:List(Vector(a, b), Vector(b, c), Vector(c, d), Vector(d, e))


list.sliding(2, 2).toList //输出:List(Vector(a, b), Vector(c, d), Vector(e))


  • 消灭零回复