[Haskell-cafe] Parsing binary 'hierachical' objects for lazy developers

John Obbele john.obbele at gmail.com
Sat Apr 30 13:40:28 CEST 2011


On Wed, Apr 27, 2011 at 09:46:08PM +0200, Maciej Marcin Piechotka wrote:
> 
> I believe following should work
> 
> class Serializer ObjectA where
>     get = check =<< (ObjectA <$> get <*> get <*> get)
>           where check obj@(ObjectA len id attr)
>                   | len < 10 && id == 0 = return obj
>                   | otherwise = empty
> 
> class Serializer ObjectB where
>     get = check =<< (ObjectB <$> get <*> get <*> get)
>           where check obj@(ObjectB len id attr)
>                   | len > 10 && id == 1 = return obj
>                   | otherwise = empty
> 
> parseEitherAB :: Get (Either ObjectA ObjectB)
> parseEitherAB = (Left <$> get) <|> (Right <$> get)


Hum, that's still not automatic but using Control.Applictive to
write one-liners seems a good-enough solution.

I will try it.

thanks ,)
/john



More information about the Haskell-Cafe mailing list