I'm trying to understand how pipes-parse 3.0 works for cases besides span
and splitAt
, and can't quite figure out how to get things working. The basic idea is that I have an isomorphism, and I'd like to map all input values to convert from type A
to type B
. Then, I'd like all leftovers to be converted back from B
to A
. How would I accomplish this in pipes-parse
?
For comparison, the code would look like the following in conduit
:
import Control.Applicative ((<$>), (<*>))
import Data.Conduit (yield, ($$), (=$=))
import Data.Conduit.Extra (fuseLeftovers)
import qualified Data.Conduit.List as CL
newtype A = A Int
deriving Show
newtype B = B Int
deriving Show
atob (A i) = (B i)
btoa (B i) = (A i)
main :: IO ()
main = do
let src = mapM_ (yield . A) [1..10]
res <- src $$ (,,,)
<$> fuseLeftovers (map btoa) (CL.map atob) CL.peek
<*> CL.take 3
<*> (CL.map atob =$= CL.take 3)
<*> CL.consume
print res
EDIT: To clarify, here's the output of my above code:
(Just (B 1),[A 1,A 2,A 3],[B 4,B 5,B 6],[A 7,A 8,A 9,A 10])
Note that the original stream is of type A
. We're converting to B
and peeking at the first element, then taking the next 3 elements as type A
, then taking the following three as B
, and finally taking the remainder as A
.