
22 Dec
2011
22 Dec
'11
3:06 p.m.
On Thursday 22 December 2011, 20:44:26, Dennis Raddle wrote:
Is there a more elegant way to write 'test' in the following?
import qualified Data.Map as M import Data.Map(Map) import Control.Monad.Error
testMap :: Map Int String testMap = M.fromList [(1, "whatever"), (2, "dude")]
test :: Int -> Either String String test x = do y <- case M.lookup x testMap of Nothing -> throwError "not in map" Just z -> return z return y
test = maybe (throwError "not in map") return . flip M.lookup testMap