Applicative combinators.
This commit is contained in:
+24
-1
@@ -5,9 +5,16 @@ module Applicative (
|
|||||||
orAlt,
|
orAlt,
|
||||||
orEmpty,
|
orEmpty,
|
||||||
eitherA,
|
eitherA,
|
||||||
|
guarded,
|
||||||
|
guardedA,
|
||||||
|
purer,
|
||||||
|
liftAA2,
|
||||||
|
(<<*>>),
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Bool (Bool)
|
import Data.Bool (Bool, bool)
|
||||||
|
import Data.Function ((.))
|
||||||
|
import Data.Functor (Functor)
|
||||||
import Data.Either (Either(..))
|
import Data.Either (Either(..))
|
||||||
import Data.Monoid (Monoid(..))
|
import Data.Monoid (Monoid(..))
|
||||||
import Control.Applicative
|
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 :: (Alternative f) => f a -> f b -> f (Either a b)
|
||||||
eitherA a b = (Left <$> a) <|> (Right <$> 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 (<*>)
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ infixl 4 $>
|
|||||||
($>) :: Functor f => f a -> b -> f b
|
($>) :: Functor f => f a -> b -> f b
|
||||||
($>) = flip (<$)
|
($>) = flip (<$)
|
||||||
|
|
||||||
|
(<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
|
||||||
|
(<<$>>) = fmap . fmap
|
||||||
|
|
||||||
void :: Functor f => f a -> f ()
|
void :: Functor f => f a -> f ()
|
||||||
void x = () <$ x
|
void x = () <$ x
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user