Safe Haskell | None |
---|---|
Language | Haskell2010 |
promoted Either
functions
boolean predicates
constructors
Left
constructor
>>>
pz @(MkLeft _ Id) 44
Val (Left 44)
Left
constructor
Right
constructor
>>>
pz @(MkRight _ Id) 44
Val (Right 44)
Right
constructor
get rid of Either
extracts the left value from an Either
>>>
pz @(Left' >> Succ) (Left 20)
Val 21
>>>
pz @(Left' >> Succ) (Right 'a')
Fail "Left' found Right"
extracts the right value from an Either
>>>
pz @(Right' >> Succ) (Right 20)
Val 21
>>>
pz @(Right' >> Succ) (Left 'a')
Fail "Right' found Left"
extract the Left value from an Either
otherwise use the default value: similar to fromLeft
if there is no Left value then p
is passed the Right value and the whole context
>>>
pz @(LeftDef (1 % 4) Id) (Left 20.4)
Val (102 % 5)
>>>
pz @(LeftDef (1 % 4) Id) (Right "aa")
Val (1 % 4)
>>>
pz @(LeftDef (PrintT "found right=%s fst=%d" '(Fst,L21)) Snd) (123,Right "xy")
Val "found right=xy fst=123"
>>>
pz @(LeftDef (MEmptyT _) Id) (Right 222)
Val ()
>>>
pz @(LeftDef (MEmptyT (SG.Sum _)) Id) (Right 222)
Val (Sum {getSum = 0})
extract the Left value from an Either
otherwise fail with a message
if there is no Left value then p
is passed the Right value and the whole context
>>>
pz @(LeftFail "oops" Id) (Left 20.4)
Val 20.4
>>>
pz @(LeftFail "oops" Id) (Right "aa")
Fail "oops"
>>>
pz @(LeftFail (PrintT "found right=%s fst=%d" '(Fst,L21)) Snd) (123,Right "xy")
Fail "found right=xy fst=123"
>>>
pz @(LeftFail (MEmptyT _) Id) (Right 222)
Fail ""
>>>
pl @(LeftFail (PrintF "someval=%d" L21) Snd) (13::Int,Right @(SG.Sum Int) "abc")
Error someval=13 (LeftFail Right) Fail "someval=13"
>>>
pl @(LeftFail (PrintF "someval=%s" Fst) Id) (Right @(SG.Sum Int) "abc")
Error someval=abc (LeftFail Right) Fail "someval=abc"
>>>
pl @(LeftFail (PrintF "found rhs=%d" Fst) Id) (Right @String @Int 10)
Error found rhs=10 (LeftFail Right) Fail "found rhs=10"
>>>
pl @(LeftFail (PrintF "found rhs=%d" (Snd >> L22)) L21) ('x',(Right 10,23))
Error found rhs=23 (LeftFail Right) Fail "found rhs=23"
>>>
pl @(LeftFail (PrintF "found rhs=%d" (L2 L22)) L21) ('x',(Left "abc",23))
Present "abc" (Left) Val "abc"
extract the Right value from an Either
: similar to fromRight
if there is no Right value then p
is passed the Left value and the whole context
>>>
pz @(RightDef (1 % 4) Id) (Right 20.4)
Val (102 % 5)
>>>
pz @(RightDef (1 % 4) Id) (Left "aa")
Val (1 % 4)
>>>
pz @(RightDef (PrintT "found left=%s fst=%d" '(Fst,L21)) Snd) (123,Left "xy")
Val "found left=xy fst=123"
>>>
pz @(RightDef (MEmptyT _) Id) (Left 222)
Val ()
>>>
pz @(RightDef (MEmptyT (SG.Sum _)) Id) (Left 222)
Val (Sum {getSum = 0})
extract the Right value from an Either
otherwise fail with a message
if there is no Right value then p
is passed the Left value and the whole context
>>>
pz @(RightFail "oops" Id) (Right 20.4)
Val 20.4
>>>
pz @(RightFail "oops" Id) (Left "aa")
Fail "oops"
>>>
pz @(RightFail (PrintT "found left=%s fst=%d" '(Fst,L21)) Snd) (123,Left "xy")
Fail "found left=xy fst=123"
>>>
pz @(RightFail (MEmptyT _) Id) (Left 222)
Fail ""
data EitherBool b p q #
Convenient method to convert a p
or q
to a Either
based on a predicate b
if b
then Right p
else Left q
>>>
pz @(EitherBool (Fst > 4) L21 L22) (24,(-1,999))
Val (Right 999)
>>>
pz @(EitherBool (Fst > 4) L21 L22) (1,(-1,999))
Val (Left (-1))
>>>
pl @(EitherBool (Fst > 10) L21 L22) (7,('x',99))
Present Left 'x' (EitherBool(False) Left 'x') Val (Left 'x')
>>>
pl @(EitherBool (Fst > 10) L21 L22) (11,('x',99))
Present Right 99 (EitherBool(True) Right 99) Val (Right 99)
>>>
pl @(EitherBool (Gt 10) "found left" 99) 12
Present Right 99 (EitherBool(True) Right 99) Val (Right 99)
>>>
pl @(EitherBool (Gt 10) "found left" 99) 7
Present Left "found left" (EitherBool(False) Left "found left") Val (Left "found left")
Instances
(Show (PP p a), P p a, Show (PP q a), P q a, P b a, PP b a ~ Bool) => P (EitherBool b p q :: Type) a # | |
Defined in Predicate.Data.Either type PP (EitherBool b p q) a :: Type # eval :: MonadEval m => proxy (EitherBool b p q) -> POpts -> a -> m (TT (PP (EitherBool b p q) a)) # | |
Show (EitherBool b p q) # | |
Defined in Predicate.Data.Either showsPrec :: Int -> EitherBool b p q -> ShowS # show :: EitherBool b p q -> String # showList :: [EitherBool b p q] -> ShowS # | |
type PP (EitherBool b p q :: Type) a # | |
Defined in Predicate.Data.Either |
data PartitionEithers #
similar to partitionEithers
>>>
pz @PartitionEithers [Left 'a',Right 2,Left 'c',Right 4,Right 99]
Val ("ac",[2,4,99])
>>>
pz @PartitionEithers [Right 2,Right 4,Right 99]
Val ([],[2,4,99])
>>>
pz @PartitionEithers [Left 'a',Left 'c']
Val ("ac",[])
>>>
pz @PartitionEithers ([] :: [Either () Int])
Val ([],[])
>>>
pl @PartitionEithers [Left 4, Right 'x', Right 'y',Left 99]
Present ([4,99],"xy") (PartitionEithers ([4,99],"xy") | [Left 4,Right 'x',Right 'y',Left 99]) Val ([4,99],"xy")
>>>
pl @PartitionEithers [Left 'x', Right 1,Left 'a', Left 'b',Left 'z', Right 10]
Present ("xabz",[1,10]) (PartitionEithers ("xabz",[1,10]) | [Left 'x',Right 1,Left 'a',Left 'b',Left 'z',Right 10]) Val ("xabz",[1,10])
Instances
Show PartitionEithers # | |
Defined in Predicate.Data.Either showsPrec :: Int -> PartitionEithers -> ShowS # show :: PartitionEithers -> String # showList :: [PartitionEithers] -> ShowS # | |
(Show a, Show b) => P PartitionEithers [Either a b] # | |
Defined in Predicate.Data.Either type PP PartitionEithers [Either a b] :: Type # eval :: MonadEval m => proxy PartitionEithers -> POpts -> [Either a b] -> m (TT (PP PartitionEithers [Either a b])) # | |
type PP PartitionEithers [Either a b] # | |
Defined in Predicate.Data.Either |
miscellaneous
similar |||
>>>
pz @(Pred ||| Id) (Left 13)
Val 12
>>>
pz @(ShowP Id ||| Id) (Right "hello")
Val "hello"
>>>
pl @('True ||| 'False) (Left "someval")
True ((|||) Left True | "someval") Val True
>>>
pl @('True ||| 'False) (Right "someval")
False ((|||) Right False | "someval") Val False
>>>
pl @(ShowP Succ ||| ShowP Id) (Left 123)
Present "124" ((|||) Left "124" | 123) Val "124"
>>>
pl @(ShowP Succ ||| ShowP Id) (Right True)
Present "True" ((|||) Right "True" | True) Val "True"
>>>
pl @(Not Id ||| Id) (Right True)
Present True ((|||) Right True | True) Val True
>>>
pl @(Not Id ||| Id) (Left True)
False ((|||) Left False | True) Val False
similar +++
>>>
pz @(Pred +++ Id) (Left 13)
Val (Left 12)
>>>
pz @(ShowP Id +++ Reverse) (Right "hello")
Val (Right "olleh")
>>>
pl @(HeadDef 'False Id +++ Id) (Right @[Bool] 1) -- need @[Bool] cos we said 'False!
Present Right 1 ((+++) Right 1 | 1) Val (Right 1)
>>>
pl @(HeadDef 'False Id +++ Id) (Left [True,False]) -- need @[Bool] cos we said 'False!
Present Left True ((+++) Left True | [True,False]) Val (Left True)
>>>
pl @(Not Id +++ Id) (Right True)
Present Right True ((+++) Right True | True) Val (Right True)
>>>
pl @(Not Id +++ Id) (Right 12)
Present Right 12 ((+++) Right 12 | 12) Val (Right 12)
>>>
pl @(HeadDef () Id +++ Id) (Right @[()] 1) -- breaks otherwise: Id says () -> () so has to be a list of [()]
Present Right 1 ((+++) Right 1 | 1) Val (Right 1)
>>>
pl @(HeadDef () Id +++ Id) (Right @[()] 1) -- this breaks! cos Left doesnt have a type
Present Right 1 ((+++) Right 1 | 1) Val (Right 1)
>>>
pl @(Not Id +++ Id) (Right @Bool 12)
Present Right 12 ((+++) Right 12 | 12) Val (Right 12)
similar to |||
but additionally gives p
and q
the original input
>>>
pz @(EitherX (ShowP (L11 + Snd)) (ShowP Id) Snd) (9,Left 123)
Val "132"
>>>
pz @(EitherX (ShowP (L11 + Snd)) (ShowP Id) Snd) (9,Right 'x')
Val "((9,Right 'x'),'x')"
>>>
pz @(EitherX (ShowP Id) (ShowP (Second Succ)) Snd) (9,Right 'x')
Val "((9,Right 'x'),'y')"