Functions from base, including few that aren't, lifted over Functor or Applicative.
These functions have proved useful to the author over the years. All of them have, at some point, being used in a production code.
Various forms of error handling are used in Haskell libraries, and applications. Some lifted functions allow us to convert between them with ease. While the syntactic overhead is bigger then using functions customized to such tasks; these functions are much more generic and so the code can be adjusted to different error handling strategies with minimal changes.
Convert Either a b
to Maybe b
where Left _
is interpreted as Nothing
:
fromRightA_ Nothing :: Either a b -> Maybe b
Convert the same Either a b
to IO b
and throw exception when Left
is encountered:
fromRightA_ (throwIO MissingValueException) :: Either a b -> IO b
-- or when:
-- MissingValueException :: String -> MissingValueException
-- then:
fromRightA (throwIO . MissingValueException) :: Either String b -> IO b
Convert Maybe a
in to Either String a
where the String
argument contains
error message in case when Nothing
is encountered:
fromJustA (Left "Error message.") :: Maybe a -> Either String a
Throw an exception on Nothing
:
fromJustA (throwIO MissingValueException) :: Maybe a -> IO a
Simplified predicates:
import Data.Bool.Lifted ((<||>))
import Data.Eq.Lifted (is)
isSquareBracket :: Char -> Bool
isSquareBracket = is '[' <||> is ']' -- Same as: (== '[') <||> (== ']')
Effectful conditions:
import System.Directory (doesFileExist, executable, getPermissions)
import Data.Bool.Lifted ((<&&>))
execute :: FilePath -> IO ()
execute fp = do
isExe <- doesFileExist fp <&&> isExecutable fp
if isExe
then do
-- -->8--
else do
-- -->8--
where
isExecutable = fmap executable . getPermissions
The BSD 3-Clause License, see LICENSE file for details.
Contributions, pull requests and bug reports are welcome! Please don't be afraid to contact author using GitHub or by e-mail.