[ bsd3, data, library ] [ Propose Tags ]

Please see the README on GitHub at https://github.com/matsubara0507/mixlogue#readme

[Skip to Readme]
Versions [RSS] [faq] 0.1.0
Change log CHANGELOG.md
Dependencies base (>=4.7 && <5), transformers [details]
License BSD-3-Clause
Copyright MATSUBARA Nobutada
Author MATSUBARA Nobutada
Maintainer t12307043@gunma-u.ac.jp
Category Data
Home page https://github.com/matsubara0507/fallible#readme
Uploaded by matsubara0507 at 2020-02-13T11:29:57Z
Distributions NixOS:0.1.0
Downloads 248 total (7 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2020-02-13 [all 1 reports]


[Index] [Quick Jump]


Maintainer's Corner

For package maintainers and hackage trustees


Readme for fallible-0.1.0

[back to package description]


Build Status

An interface for fallible data types like Maybe and Either.


Dealing with Maybe and Either gets a bit annoying when monadic actions are involved, often resulting in deep nests like this:

run :: String -> Token -> Bool -> IO ()
run targetName token verbose = do
  users <- getUsers token
  case users of
    Left err -> logDebug' err
    Right us -> do
      case userId <$> L.find isTarget us of
        Nothing  -> logDebug' emsg
        Just tid -> do
          channels <- getChannels token
          case channels of
            Left err  -> logDebug' err
            Right chs -> do
              let chs' = filter (elem tid . channelMembers) chs
              mapM_ (logDebug' . channelName) chs'

This package offers you several combinators to tidy up this eyesore. See example/Main.hs:

import           Data.Fallible
import qualified Data.List     as L

run :: String -> Token -> Bool -> IO ()
run targetName token verbose = evalContT $ do
  users    <- lift (getUsers token) !?= exit . logDebug'
  targetId <- userId <$> L.find isTarget users ??? exit (logDebug' emsg)
  channels <- lift (getChannels token) !?= exit . logDebug'
  lift $ mapM_ (logDebug' . channelName) $
    filter (elem targetId . channelMembers) channels
    logDebug' = logDebug verbose
    emsg = "user not found: " ++ targetName
    isTarget user = userName user == targetName

logDebug :: Bool -> String -> IO ()
logDebug verbose msg = if verbose then putStrLn msg else pure ()

Notice the couple of operators, (!?=) and (???):

(!?=) :: Monad m => m (Either e a) -> (e -> m a) -> m a
(!??) :: Monad m => m (Maybe a) -> m a -> m a
(??=) :: Applicative f => Either e a -> (e -> f a) -> f a
(???) :: Applicative f => Maybe a -> f a -> f a

As you can guess from their type signatures, they run the right side when the first arguments returns Left or Nothing. Then you can use whatever you like to handle failures -- exception, MaybeT, ExceptT, ContT etc.

exec using ghci:

$ stack ghci
>>> :l example/Main.hs
*Main> run "Alice" "dummy" True
*Main> run "Curry" "dummy" True
*Main> run "Haskell" "dummy" True
user not found: Haskell
*Main> run "Haskell" "" True
invalid token

Become a beta tester


write to stack.yaml:

  github: matsubara0507/fallible
  commit: XXXXXXX


  type: git
  location: https://github.com/matsubara0507/fallible
  tag: XXXXXXX