Skip to content

Commit

Permalink
add utilities for interacting with HO state
Browse files Browse the repository at this point in the history
  • Loading branch information
tek committed Sep 23, 2023
1 parent 582ae1c commit 4a0bc8a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Polysemy/HigherOrder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 #-}

0 comments on commit 4a0bc8a

Please sign in to comment.