Applicative combinators.

This commit is contained in:
Stephen Diehl
2016-10-14 10:56:37 +01:00
parent a2e97dc936
commit c061df8f06
2 changed files with 27 additions and 1 deletions
+24 -1
View File
@@ -5,9 +5,16 @@ module Applicative (
orAlt,
orEmpty,
eitherA,
guarded,
guardedA,
purer,
liftAA2,
(<<*>>),
) where
import Data.Bool (Bool)
import Data.Bool (Bool, bool)
import Data.Function ((.))
import Data.Functor (Functor)
import Data.Either (Either(..))
import Data.Monoid (Monoid(..))
import Control.Applicative
@@ -20,3 +27,19 @@ orEmpty b a = if b then pure a else empty
eitherA :: (Alternative f) => f a -> f b -> f (Either a b)
eitherA a b = (Left <$> a) <|> (Right <$> b)
guarded :: (Alternative f) => (a -> Bool) -> a -> f a
guarded p x = bool empty (pure x) (p x)
guardedA :: (Functor f, Alternative t) => (a -> f Bool) -> a -> f (t a)
guardedA p x = bool empty (pure x) <$> p x
purer :: (Applicative f, Applicative g) => a -> f (g a)
purer = pure . pure
liftAA2 :: (Applicative f, Applicative g) => (a -> b -> c) -> f (g a) -> f (g b) -> f (g c)
liftAA2 = liftA2 . liftA2
(<<*>>) :: (Applicative f, Applicative g) => f (g (a -> b)) -> f (g a) -> f (g b)
(<<*>>) = liftA2 (<*>)
+3
View File
@@ -29,6 +29,9 @@ infixl 4 $>
($>) :: Functor f => f a -> b -> f b
($>) = flip (<$)
(<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
(<<$>>) = fmap . fmap
void :: Functor f => f a -> f ()
void x = () <$ x
#endif