是否可以在Conduit中构建一个函数(例如zipC2),该函数将转换以下来源:
series1 = yieldMany [2, 4, 6, 8, 16 :: Int]
series2 = yieldMany [1, 5, 6 :: Int]
变成一对,将产生以下对(如列表所示):
[(Nothing, Just 1), (Just 2, Just 1), (Just 4, Just 1), (Just 4, Just 5), (Just 6, Just 6), (Just 8, Just 6), (Just 16, Just 6)]
可以使用比较功能通过以下方式调用它:
runConduitPure ( zipC2 (<=) series1 series1 .| sinkList )
以前的版本中曾经有一个mergeSources
函数,其功能相对相似(尽管没有记忆效应),但是在最新版本(1.3.1)中却消失了。
功能说明: 想法是采用两个来源 A (生成值 a )和 B (生成值 b )。 / p>
然后我们生成对:
如果 a 我们首先构建(只是什么也没有)
如果 b 会产生(无,只是b)
如果 a == b 我们更新双方,并生成(Just a,Just b)
来自源的未更新的值不会被消耗,并用于下一轮比较。仅使用更新的值。
然后根据 A 和 B 相对于彼此的值,继续更新该对。
换句话说:如果 a ,则更新对的左侧;如果 b ,则更新对的左侧;如果 a,则更新双方的对侧== b 。任何未消耗的值都将保留在内存中,以进行下一轮比较。
答案 0 :(得分:1)
下面的代码按预期工作(我称为功能mergeSort):
module Data.Conduit.Merge where
import Prelude (Monad, Bool, Maybe(..), Show, Eq)
import Prelude (otherwise, return)
import Prelude (($))
import Conduit (ConduitT)
import Conduit (evalStateC, mapC, yield, await)
import Conduit ((.|))
import Control.Monad.State (get, put, lift)
import Control.Monad.Trans.State.Strict (StateT)
import qualified Data.Conduit.Internal as CI
-- | Takes two sources and merges them.
-- This comes from https://github.com/luispedro/conduit-algorithms made available thanks to Luis Pedro Coelho.
mergeC2 :: (Monad m) => (a -> a -> Bool) -> ConduitT () a m () -> ConduitT () a m () -> ConduitT () a m ()
mergeC2 comparator (CI.ConduitT s1) (CI.ConduitT s2) = CI.ConduitT $ processMergeC2 comparator s1 s2
processMergeC2 :: Monad m => (a -> a -> Bool)
-> ((() -> CI.Pipe () () a () m ()) -> CI.Pipe () () a () m ()) -- s1 ConduitT () a m ()
-> ((() -> CI.Pipe () () a () m ()) -> CI.Pipe () () a () m ()) -- s2 ConduitT () a m ()
-> ((() -> CI.Pipe () () a () m b ) -> CI.Pipe () () a () m b ) -- rest ConduitT () a m ()
processMergeC2 comparator s1 s2 rest = go (s1 CI.Done) (s2 CI.Done)
where
go s1''@(CI.HaveOutput s1' v1) s2''@(CI.HaveOutput s2' v2) -- s1''@ and s2''@ simply name the pattern expressions
| comparator v1 v2 = CI.HaveOutput (go s1' s2'') v1
| otherwise = CI.HaveOutput (go s1'' s2') v2
go s1'@CI.Done{} (CI.HaveOutput s v) = CI.HaveOutput (go s1' s) v
go (CI.HaveOutput s v) s1'@CI.Done{} = CI.HaveOutput (go s s1') v
go CI.Done{} CI.Done{} = rest ()
go (CI.PipeM p) left = do
next <- lift p
go next left
go right (CI.PipeM p) = do
next <- lift p
go right next
go (CI.NeedInput _ next) left = go (next ()) left
go right (CI.NeedInput _ next) = go right (next ())
go (CI.Leftover next ()) left = go next left
go right (CI.Leftover next ()) = go right next
data MergeTag = LeftItem | RightItem deriving (Show, Eq)
data TaggedItem a = TaggedItem MergeTag a deriving (Show, Eq)
mergeTag :: (Monad m) => (a -> a -> Bool) -> ConduitT () a m () -> ConduitT () a m () -> ConduitT () (TaggedItem a) m ()
mergeTag func series1 series2 = mergeC2 (tagSort func) taggedSeries1 taggedSeries2
where
taggedSeries1 = series1 .| mapC (\item -> TaggedItem LeftItem item)
taggedSeries2 = series2 .| mapC (\item -> TaggedItem RightItem item)
tagSort :: (a -> a -> Bool) -> TaggedItem a -> TaggedItem a -> Bool
tagSort f (TaggedItem _ item1) (TaggedItem _ item2) = f item1 item2
type StateMergePair a = (Maybe a, Maybe a)
pairTagC :: (Monad m) => ConduitT (TaggedItem a) (StateMergePair a) (StateT (StateMergePair a) m) ()
pairTagC = do
input <- await
case input of
Nothing -> return ()
Just taggedItem -> do
stateMergePair <- lift get
let outputState = updateStateMergePair taggedItem stateMergePair
lift $ put outputState
yield outputState
pairTagC
updateStateMergePair :: TaggedItem a -> StateMergePair a -> StateMergePair a
updateStateMergePair (TaggedItem tag item) (Just leftItem, Just rightItem) = case tag of
LeftItem -> (Just item, Just rightItem)
RightItem -> (Just leftItem, Just item)
updateStateMergePair (TaggedItem tag item) (Nothing, Just rightItem) = case tag of
LeftItem -> (Just item, Just rightItem)
RightItem -> (Nothing, Just item)
updateStateMergePair (TaggedItem tag item) (Just leftItem, Nothing) = case tag of
LeftItem -> (Just item, Nothing)
RightItem -> (Just leftItem, Just item)
updateStateMergePair (TaggedItem tag item) (Nothing, Nothing) = case tag of
LeftItem -> (Just item, Nothing)
RightItem -> (Nothing, Just item)
pairTag :: (Monad m) => ConduitT (TaggedItem a) (StateMergePair a) m ()
pairTag = evalStateC (Nothing, Nothing) pairTagC
mergeSort :: (Monad m) => (a -> a -> Bool) -> ConduitT () a m () -> ConduitT () a m () -> ConduitT () (StateMergePair a) m ()
mergeSort func series1 series2 = mergeTag func series1 series2 .| pairTag
我从https://github.com/luispedro/conduit-algorithms借来了mergeC2函数...
我只是Haskell的初学者,因此代码肯定不是最佳的。