How can I express the following without the nested case statements? Specifically, I would like to do the steps in order, and call fail if any of the steps hit the "bad" branch.
loadData :: IO [Market]
loadData = do
filedata <- B.readFile "markets.json"
case parseData filedata of
Left err -> fail err
Right v -> do
case fromJSON v of
Error err -> fail err
Success a -> return a
You probably want the exception monad transformer here. Something like this (utterly untested):
import Control.Monad.Trans
import Control.Monad.Except
type MyMonadStack a = ExceptT String IO a
loadData :: MyMonadStack [Market]
loadData = do filedata <- lift $ B.readFile "markets.json"
v <- ExceptT $ parseData filedata
a <- ExceptT $ toEither $ fromJSON v
return a
toEither :: WhateverTypeFromJSONReturns a b -> Either a b
toEither (Error a) = Left a
toEither (Success b) = Right b
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments