Re: [Haskell-Cafe] Parsing bytestream

2010-11-15 Thread C K Kashyap
Hi Felipe, On Tue, Nov 9, 2010 at 3:53 PM, Felipe Almeida Lessa felipe.le...@gmail.com wrote: On Tue, Nov 9, 2010 at 8:10 AM, C K Kashyap ckkash...@gmail.com wrote: ] I think I can restate my problem like this --- ] ] If I have a list of actions as follows - ] ] import Data.Word ] import

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread Stephen Tetley
I'd use a parser combinator library that has word8 word16, word32 combinators. The latter should really have big and little endian versions word16be, word16le, word32be, word32le. Data.Binary should provide this and Attoparsec I think. Usually I roll my own, but only because I had my own

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread C K Kashyap
Thanks Stephen, On Tue, Nov 9, 2010 at 2:53 PM, Stephen Tetley stephen.tet...@gmail.com wrote: I'd use a parser combinator library that has word8 word16, word32 combinators. The latter should really have big and little endian versions word16be, word16le, word32be, word32le. Data.Binary

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread Vo Minh Thu
2010/11/9 C K Kashyap ckkash...@gmail.com: Thanks Stephen, On Tue, Nov 9, 2010 at 2:53 PM, Stephen Tetley stephen.tet...@gmail.com wrote: I'd use a parser combinator library that has word8 word16, word32 combinators. The latter should really have big and little endian versions word16be,

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread C K Kashyap
I think I can restate my problem like this --- If I have a list of actions as follows - import Data.Word import Data.Binary.Get data MyAction = A1 (Get Word8) | A2 (Get Word16) a = A1 getWord8 b = A2 getWord16be listOfActions = [a,b,a] How can I execute the listOfActions inside of a Get

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread Vo Minh Thu
2010/11/9 C K Kashyap ckkash...@gmail.com: I think I can restate my problem like this --- If I have a list of actions as follows - import Data.Word import Data.Binary.Get data MyAction = A1 (Get Word8) | A2 (Get Word16) a = A1 getWord8 b = A2 getWord16be listOfActions = [a,b,a] How

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread Felipe Almeida Lessa
On Tue, Nov 9, 2010 at 8:10 AM, C K Kashyap ckkash...@gmail.com wrote: ] I think I can restate my problem like this --- ] ] If I have a list of actions as follows - ] ] import Data.Word ] import Data.Binary.Get ] ] data MyAction = A1 (Get Word8) | A2 (Get Word16) ] ] a = A1 getWord8 ] b = A2

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread Ozgur Akgun
If we change the code a bit, data MyAction = A1 Word8 | A2 Word16 a,b :: Get MyAction a = A1 $ getWord8 b = A2 $ getWord16be listOfActions :: [Get MyAction] listOfActions = [a,b,a] Now, we know how to execute the list of actions, and get the output as list. Using the following guys:

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread Vo Minh Thu
2010/11/9 Ozgur Akgun ozgurak...@gmail.com: If we change the code a bit, data MyAction = A1 Word8 | A2 Word16 a,b :: Get MyAction a = A1 $ getWord8 b = A2 $ getWord16be listOfActions :: [Get MyAction] listOfActions = [a,b,a] Now, we know how to execute the list of actions, and get the

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread C K Kashyap
The original question was (I believe) how to drive the parsing with a list of Actions, not the result be a list of Actions. Yes ... the result needs to be a list of plain values - Int's But I did not follow why you have dropped the word16 -- Regards, Kashyap

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread C K Kashyap
Thanks Ozgur and Felipe, Could you also show how I could actually use it to parse a bytestring please? import qualified Data.ByteString.Lazy as BS import Control.Applicative (($)) import Data.Word import Data.Binary.Get data MyAction = A1 Word8 | A2 Word16 a,b :: Get MyAction a = A1 $

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread C K Kashyap
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

Re: [Haskell-Cafe] Parsing bytestream

2010-11-09 Thread C K Kashyap
Oops .. I made a mistake .. I had gone with Felipe's solution - getActions :: [MyAction Get] - Get [MyAction Id] getActions = mapM getAction -- Felipe. Felpe, could you please confirm if bs = BS.pack [1,0,2,4] toVal (A1 (Id v)) = fromIntegral v :: Int toVal (A2 (Id v)) = fromIntegral