diff --git a/src/Polysemy/HigherOrder.hs b/src/Polysemy/HigherOrder.hs index 7ffd448e..884d1d13 100644 --- a/src/Polysemy/HigherOrder.hs +++ b/src/Polysemy/HigherOrder.hs @@ -71,6 +71,34 @@ module Polysemy.HigherOrder -- * Retrieving the type parameters of a 'HigherOrder' , TypeParamsH(..) , getTypeParamsH + + -- * Interact with reified higher-order state + , discardStateWith + , discardState + , discardStateEither + , discardStateMaybe ) where import Polysemy.Internal.HigherOrder + +-- | Get the value returned by a higher-order action that exposes the reified higher-order state. +-- +-- This is needed, for example, when you want to execute multiple actions in a 'controlH' block +-- that depend on the result of earlier actions. +-- +-- @since 2.0.0.0 +discardStateWith :: Traversable t => b -> (a -> b) -> t a -> b +discardStateWith alt f = foldr (const . f) alt +{-# inline discardStateWith #-} + +discardState :: Traversable t => a -> t a -> a +discardState alt = foldr const alt +{-# inline discardState #-} + +discardStateEither :: Traversable t => b -> t a -> Either b a +discardStateEither alt = discardStateWith (Left alt) Right +{-# inline discardStateEither #-} + +discardStateMaybe :: Traversable t => t a -> Maybe a +discardStateMaybe = discardStateWith Nothing Just +{-# inline discardStateMaybe #-}