有没有一种惯用的方式来分块和连接?
我发现的方式(字节示例):
-
1.
import scodec.bits.ByteVector
def byteChunk(n: Int): Process1[ByteVector, ByteVector] =
process1.chunk(n).map(_.reduce(_ ++ _))
但在这种情况下,并不真正需要中间体Vector
(from )。chunk
- 基于 process1.chunk 的复制/粘贴:
def byteChunk(n: Int): Process1[ByteVector, ByteVector] = {
def go(m: Int, acc: ByteVector): Process1[ByteVector, ByteVector] =
if (m <= 0) {
emit(acc) ++ go(n, ByteVector.empty)
} else {
def fallback = if (acc.nonEmpty) emit(acc) else halt
receive1Or[ByteVector, ByteVector](fallback) { in =>
go(m - 1, acc ++ in)
}
}
go(n, ByteVector.empty)
}
Process
有没有办法通过组合现有的'es来做同样的事情?
一个附带问题:可以repeat
用来代替++ go
? 是不是和上一个一样:
def byteChunk(n: Int): Process1[ByteVector, ByteVector] = {
def go(m: Int, acc: ByteVector): Process1[ByteVector, ByteVector] =
if (m <= 0) emit(acc)
else ...
go(n, ByteVector.empty).repeat
}