Skip to content

Commit

Permalink
Merge pull request #3 from haskell-works/newhoggy/new-error-handling-…
Browse files Browse the repository at this point in the history
…functions

New error handling functions
  • Loading branch information
newhoggy authored Nov 5, 2024
2 parents 667f7a8 + fa666d7 commit b420b96
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 14 deletions.
107 changes: 93 additions & 14 deletions src/HaskellWorks/Error.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,107 @@
module HaskellWorks.Error
( onLeft,
onNothing,
onLeftM,
( onNothing,
onNothingM,
onLeft,
onLeftM,
onLeft_,
onLeftM_,
onMany,
onManyM,
onMany_,
onManyM_,
) where

import HaskellWorks.Prelude

onLeft :: forall e a m. ()
=> Monad m
=> (e -> m a) -> Either e a -> m a
onLeft f = either f pure
import Control.Applicative
import Control.Monad
import Data.Either
import Data.Function
import Data.List.NonEmpty
import Data.Maybe

-- | Handle the case where a value is 'Nothing'.
onNothing :: forall a m. ()
=> Monad m
=> m a -> Maybe a -> m a
=> m a
-> Maybe a
-> m a
onNothing h = maybe h return

-- | Handle the case where an effectful function returns 'Nothing'.
onNothingM :: forall a m. ()
=> Monad m
=> m a
-> m (Maybe a)
-> m a
onNothingM h f = onNothing h =<< f

-- | Handle the case where a value is 'Left'.
onLeft :: forall e a m. ()
=> Monad m
=> (e -> m a)
-> Either e a
-> m a
onLeft f = either f pure

-- | Handle the case where an effectful function returns 'Left'.
onLeftM :: forall e a m. ()
=> Monad m
=> (e -> m a) -> m (Either e a) -> m a
=> (e -> m a)
-> m (Either e a)
-> m a
onLeftM f action = onLeft f =<< action

onNothingM :: forall a m. ()
-- | Handle the case where a value is 'Left'.
onLeft_ :: forall e a m. ()
=> Monad m
=> m a -> m (Maybe a) -> m a
onNothingM h f = onNothing h =<< f
=> m a
-> Either e a
-> m a
onLeft_ f = either (const f) pure

-- | Handle the case where an effectful function returns 'Left'.
onLeftM_ :: forall e a m. ()
=> Monad m
=> m a
-> m (Either e a)
-> m a
onLeftM_ f action = onLeft_ f =<< action

-- | Handle the case where a list with many (more than one) elements.
onMany :: forall a m.()
=> Monad m
=> (NonEmpty a -> m (Maybe a))
-> [a]
-> m (Maybe a)
onMany h as = case as of
[] -> pure Nothing
[x] -> pure (Just x)
(x : xs) -> h (x :| xs)

-- | Handle the case where an effectul function returns a list with many (more than one) elements.
onManyM :: forall a m.()
=> Monad m
=> (NonEmpty a -> m (Maybe a))
-> m [a]
-> m (Maybe a)
onManyM h f =
f >>= onMany h

-- | Handle the case where a list with many (more than one) elements.
onMany_ :: forall a m.()
=> Monad m
=> m (Maybe a)
-> [a]
-> m (Maybe a)
onMany_ h as = case as of
[] -> pure Nothing
[x] -> pure (Just x)
_ -> h

-- | Handle the case where an effectul function returns a list with many (more than one) elements.
onManyM_ :: forall a m.()
=> Monad m
=> m (Maybe a)
-> m [a]
-> m (Maybe a)
onManyM_ h f =
f >>= onMany_ h
12 changes: 12 additions & 0 deletions src/HaskellWorks/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ module HaskellWorks.Prelude
IOException,
IOError,
SomeException(..),

onNothing,
onNothingM,
onLeft,
onLeftM,
onLeft_,
onLeftM_,
onMany,
onManyM,
onMany_,
onManyM_,
) where

import Control.Applicative
Expand Down Expand Up @@ -196,6 +207,7 @@ import GHC.Generics
import GHC.Num
import GHC.Real
import GHC.Stack
import HaskellWorks.Error
import Prelude
import System.FilePath

Expand Down

0 comments on commit b420b96

Please sign in to comment.