guarded and guardedA added to root.

This commit is contained in:
Stephen Diehl
2016-10-14 11:03:37 +01:00
parent c061df8f06
commit a86046b654
2 changed files with 10 additions and 11 deletions
+1 -10
View File
@@ -5,16 +5,13 @@ module Applicative (
orAlt, orAlt,
orEmpty, orEmpty,
eitherA, eitherA,
guarded,
guardedA,
purer, purer,
liftAA2, liftAA2,
(<<*>>), (<<*>>),
) where ) where
import Data.Bool (Bool, bool) import Data.Bool (Bool)
import Data.Function ((.)) 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
@@ -28,12 +25,6 @@ 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 :: (Applicative f, Applicative g) => a -> f (g a)
purer = pure . pure purer = pure . pure
+9 -1
View File
@@ -20,7 +20,8 @@ module Protolude (
foreach, foreach,
show, show,
pass, pass,
guarded,
guardedA,
LText, LText,
LByteString, LByteString,
) where ) where
@@ -404,6 +405,13 @@ foreach = flip fmap
pass :: Applicative f => f () pass :: Applicative f => f ()
pass = pure () pass = pure ()
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
show :: (Show a, StringConv String b) => a -> b show :: (Show a, StringConv String b) => a -> b
show x = toS (PBase.show x) show x = toS (PBase.show x)
{-# SPECIALIZE show :: Show a => a -> Text #-} {-# SPECIALIZE show :: Show a => a -> Text #-}