2015-03-03 43 views
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 
    } 
    

    回答

    0

    我認爲你可以簡化這個有點用takelastscanMonoid

    def byteChunk(n: Int): Process1[ByteVector, ByteVector] = { 
        process1.take[ByteVector](n).scanMonoid.last ++ Process.suspend(go(n)) 
    } 
    

    (或類似.scan(ByteVector.empty)((acc, x: ByteVector) => acC++ x)更換scanMonoid,如果你不希望實施Monoid[ByteVector])。

    似乎Process.repeat將代替++ Process.suspend(go(n))而且實施Process.repeat的方式暗示只要byteChunk(n)是純的就會成立。