Is there an idiomatic way to chunk and concatenate?
The ways that I've found (examples for bytes):
-
1.
import scodec.bits.ByteVector
def byteChunk(n: Int): Process1[ByteVector, ByteVector] =
process1.chunk(n).map(_.reduce(_ ++ _))
But the intermediate Vector
(from chunk
) isn't really needed in this case.
- Based on copy/paste from 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)
}
Is there a way to do the same with combining the existing Process
'es?
A side question: could repeat
be used instead of ++ go
? Is this the same as the previous:
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
}