module Control.Eff.Example where
import Control.Eff
import Control.Eff.State.Lazy
import Control.Eff.Writer.Lazy
import Data.Typeable
writeAll :: (Typeable a, Member (Writer a) e)
=> [a]
-> Eff e ()
writeAll = mapM_ tell
sumAll :: (Typeable a, Num a, Member (State a) e)
=> [a]
-> Eff e ()
sumAll = mapM_ (modify . (+))
writeAndAdd :: (Member (Writer a) e, Member (State a) e, Num a, Typeable a)
=> [a]
-> Eff e ()
writeAndAdd l = do
writeAll l
sumAll l
sumEff :: (Num a, Typeable a) => [a] -> a
sumEff l = let (s, ()) = run $ runState 0 $ sumAll l
in s
lastEff :: Typeable a => [a] -> Maybe a
lastEff l = let (a, ()) = run $ runLastWriter $ writeAll l
in a
lastAndSum :: (Typeable a, Num a) => [a] -> (Maybe a, a)
lastAndSum l = let (lst, (total, ())) = run $ runLastWriter $ runState 0 $ writeAndAdd l
in (lst, total)