3 ms·
Sure, you can improve this by e.g. import Data.Either.Combinators -- from the either package strSum :: String -> Either String Int strSum = fmap sum . s
by Tainnor 2y ago
Sure, you can improve this by e.g.
import Data.Either.Combinators -- from the either package
strSum :: String -> Either String Int
strSum = fmap sum . sequence . fmap tryReadInt . words
where
tryReadInt w = maybeToRight ("not an integer " ++ w) (readMaybe w)
This keeps the bulk of the method the same. Being able to go e.g. from Maybe to Either with only few changes (as long as your code is sufficiently general) is one of the nice things you get from all the Haskell abstraction. You can't really do that if you start with exceptions (unless you're in IO).