Haskell 重构管道接口
我正在使用管道库(4.1.4)组装一个编码器模型 我有一个工作功能Haskell 重构管道接口,haskell,refactoring,pipe,Haskell,Refactoring,Pipe,我正在使用管道库(4.1.4)组装一个编码器模型 我有一个工作功能 encode :: (Monad m) => Int -> - -- number of codes per bit Producer Word32 m r -> -- code source Producer Word32 m r -> -- data source
encode :: (Monad m) => Int -> - -- number of codes per bit
Producer Word32 m r -> -- code source
Producer Word32 m r -> -- data source
Producer Word32 m r -- resulting stream
encode n cp dp = P.zipWith xor cp (dp >-> (upsample n))
我想把它重构为
encode :: (Monad m) => Int ->
Producer Word32 m r ->
Pipe Word32 m r
我认为它的用法更清楚;它对流经管道的数据进行编码。我一点也不知道如何做到这一点
编辑
我想你需要这个:
encode n cp = upsample n >-> f cp where
f c = do
r <- lift $ next c
case r of
Left r -> return r -- use f cp here, if you really meant Viginere
Right (x, c') -> do
y <- await
yield $ x `xor` y
f c'
您是否希望以某种方式组合这两个
Producer
参数?我想部分应用一个代码生成器,剩下一个pipeDoesencode n cp=(c upsample n
工作吗?可能可以编写一个通用函数(Monad m)=>(Producer a m r->Producer b m r)->Proxy()a()b m r
。我不确定它是否需要对请求或返回进行阻塞,或者它是否需要从底层的Monad
@OlliB获得更多。我提出了另一个问题。
encode n cp = upsample n >-> f cp where
f c = do
r <- lift $ next c
case r of
Left r -> return r -- use f cp here, if you really meant Viginere
Right (x, c') -> do
y <- await
yield $ x `xor` y
f c'
upsample :: Monad m => Int -> Pipe Int Int m r
upsample n = do
x<-await
yield $ -x
upsample n
encode n cp = (upsample n) >-> f cp where
f cp = do
r <- lift $ next cp
case r of
Left r -> return r
Right (x,cp) -> do
y <- await
yield $ x*y -- I was lazy to import Bits
f cp
Prelude Pipes> runEffect $ mapM_ yield [0..] >->
encode 10 (mapM_ yield [1..10]) >->
sequence_ (repeat $ do {x <- await; lift $ print x})
0
-2 -- remember, [0..] is negated by upsample, then multiplied by [1..10]
-6
-12
-20
-30
-42
-56
-72
-90 -- only 10 items were available in `cp`, so only 10 items printed