我有一个 unordered 流measurements
,我想将其分组为固定大小的批次,以便以后可以有效地保留它们:
val measurements = for {
id <- Seq("foo", "bar", "baz")
value <- 1 to 5
} yield (id, value)
fs2.Stream.emits(scala.util.Random.shuffle(measurements)).toVector
也就是说,而不是:
(bar,4)
(foo,5)
(baz,3)
(baz,5)
(baz,4)
(foo,2)
(bar,2)
(foo,4)
(baz,1)
(foo,1)
(foo,3)
(bar,1)
(bar,5)
(bar,3)
(baz,2)
我希望批量大小等于以下结构3
:
(bar,[4,2,1])
(foo,[5,2,4])
(baz,[3,5,4])
(baz,[1,2])
(foo,[1,3])
(bar,[5,3])
在 FS2 中是否有一种简单、惯用的方法来实现这一点?我知道有一个groupAdjacentBy函数,但这只会考虑相邻的项目。
我现在在上0.10.5
。