Useful Functor product and coproducts

Could you show a simple code example that would display the useful Data.Functor Product and Coproduct ?

+6
source share
2 answers

A Product of Const and a Reader can be used to easily implement a two-step evaluation. For example, suppose you need to use some kind of monadic effect between the two phases, but you want to make sure that your client code cannot do this (because you want to precisely control how and when this happens):

 type TwoPhase cr = Product (Const c) (Reader a) run :: (Monad m, Monoid c) => (c -> mr) -> TwoPhase cra -> ma run prepare (Pair (Const deps, phase2)) = do r <- prepare deps return $ runReader r 

Note that this, of course, enables the Applicative interface for your API, and not monodic; but what you usually want anyway in such a situation.

+7
source

A possible use of the coproduct functor is used in a la carte data types . The idea is to use coproducts to combine sibling data type constructors.

+3
source

All Articles