mirror of
https://github.com/polysemy-research/polysemy.git
synced 2024-11-23 09:15:22 +03:00
add Applicative methods (#372)
* add Applicative methods - add Applicative `liftA2`, `(<*)`, and `(*>)` - resolves https://github.com/polysemy-research/polysemy/issues/368 * remove redundant Monad return definition - As directed in review of https://github.com/polysemy-research/polysemy/pull/372
This commit is contained in:
parent
235813da00
commit
de19721ef5
@ -271,11 +271,18 @@ instance Applicative (Sem f) where
|
||||
Sem f <*> Sem a = Sem $ \k -> f k <*> a k
|
||||
{-# INLINE (<*>) #-}
|
||||
|
||||
liftA2 f ma mb = Sem $ \k -> liftA2 f (runSem ma k) (runSem mb k)
|
||||
{-# INLINE liftA2 #-}
|
||||
|
||||
ma <* mb = Sem $ \k -> runSem ma k <* runSem mb k
|
||||
{-# INLINE (<*) #-}
|
||||
|
||||
-- Use (>>=) because many monads are bad at optimizing (*>).
|
||||
-- Ref https://github.com/polysemy-research/polysemy/issues/368
|
||||
ma *> mb = Sem $ \k -> runSem ma k >>= \_ -> runSem mb k
|
||||
{-# INLINE (*>) #-}
|
||||
|
||||
instance Monad (Sem f) where
|
||||
return = pure
|
||||
{-# INLINE return #-}
|
||||
|
||||
Sem ma >>= f = Sem $ \k -> do
|
||||
z <- ma k
|
||||
runSem (f z) k
|
||||
|
Loading…
Reference in New Issue
Block a user