@@ -3,7 +3,6 @@ module Data.These where
33import Prelude
44
55import Control.Extend (class Extend )
6-
76import Data.Bifunctor (class Bifunctor )
87import Data.Bitraversable (class Bitraversable , class Bifoldable , bitraverse )
98import Data.Functor.Invariant (class Invariant , imapF )
@@ -112,17 +111,39 @@ thatOrBoth :: forall a b. b -> Maybe a -> These a b
112111thatOrBoth b Nothing = That b
113112thatOrBoth b (Just a) = Both a b
114113
114+ -- | Takes a pair of `Maybe`s and attempts to create a `These` from them.
115+ maybeThese :: forall a b . Maybe a -> Maybe b -> Maybe (These a b )
116+ maybeThese = case _, _ of
117+ Just a, Nothing -> Just (This a)
118+ Nothing , Just b -> Just (That b)
119+ Just a, Just b -> Just (Both a b)
120+ Nothing , Nothing -> Nothing
121+
115122fromThese :: forall a b . a -> b -> These a b -> Tuple a b
116123fromThese _ x (This a) = Tuple a x
117124fromThese a _ (That x) = Tuple a x
118125fromThese _ _ (Both a x) = Tuple a x
119126
127+ -- | Returns an `a` value if possible.
120128theseLeft :: forall a b . These a b -> Maybe a
121129theseLeft (Both x _) = Just x
122130theseLeft (This x) = Just x
123131theseLeft _ = Nothing
124132
133+ -- | Returns a `b` value if possible.
125134theseRight :: forall a b . These a b -> Maybe b
126135theseRight (Both _ x) = Just x
127136theseRight (That x) = Just x
128137theseRight _ = Nothing
138+
139+ -- | Returns the `a` value if and only if the value is constructed with `This`.
140+ this :: forall a b . These a b -> Maybe a
141+ this = case _ of
142+ This x -> Just x
143+ _ -> Nothing
144+
145+ -- | Returns the `b` value if and only if the value is constructed with `That`.
146+ that :: forall a b . These a b -> Maybe b
147+ that = case _ of
148+ That x -> Just x
149+ _ -> Nothing
0 commit comments