[Haskell-Cafe] Parsing bytestream
C K Kashyap
ckkashyap at gmail.com
Tue Nov 9 06:29:09 EST 2010
Okay, I think I got it .. I went with Ozgur's example -
data MyAction m = A1 (m Word8) | A2 (m Word16)
a = A1 getWord8
b = A2 getWord16be
listOfActions = [a,b,a]
newtype Id a = Id a
getAction :: MyAction Get -> Get (MyAction Id)
getAction (A1 act) = A1 . Id <$> act
getAction (A2 act) = A2 . Id <$> act
getActions :: [MyAction Get] -> Get [MyAction Id]
getActions = mapM getAction
bs = BS.pack [1,0,2,4]
abc = map toVal $ runGet (getActions listOfActions) bs
toVal (A1 (Id v)) = fromIntegral v :: Int
toVal (A2 (Id v)) = fromIntegral v :: Int
Kindly let me know if this is exactly how you intended it to be used?
Regards,
Kashyap
More information about the Haskell-Cafe
mailing list