1
有沒有一種慣用的方式來塊和連接?是不是真的需要在這種情況下scalaz-stream:如何連接串聯?
-
1.
import scodec.bits.ByteVector
def byteChunk(n: Int): Process1[ByteVector, ByteVector] =
process1.chunk(n).map(_.reduce(_ ++ _))
但中間Vector
(從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
}