@@ -10,6 +10,8 @@ import Data.Maybe (Maybe(..), isJust)
1010import Data.Traversable (class Traversable , class Foldable , foldMap , foldl , foldr )
1111import Data.Tuple (Tuple (..))
1212
13+ -- | Data type isomorphic to `α ∨ β ∨ (α ∧ β)` or
14+ -- | `Either a (Either b (Tuple a b))`.
1315data These a b
1416 = This a
1517 | That b
@@ -98,6 +100,8 @@ instance showThese :: (Show a, Show b) => Show (These a b) where
98100 show (That y) = " (That " <> show y <> " )"
99101 show (Both x y) = " (Both " <> show x <> " " <> show y <> " )"
100102
103+ -- | Given functions to handle each constructor, collapse a `These` value
104+ -- | into single value.
101105these :: forall a b c . (a -> c ) -> (b -> c ) -> (a -> b -> c ) -> These a b -> c
102106these l _ _ (This a) = l a
103107these _ r _ (That x) = r x
@@ -119,6 +123,10 @@ maybeThese = case _, _ of
119123 Just a, Just b -> Just (Both a b)
120124 Nothing , Nothing -> Nothing
121125
126+ -- | Takes two default values and a `These` value. If the `These` value is
127+ -- | `This` or `That`, the value wrapped in the `These` value and its
128+ -- | corresponding default value are wrapped into a `Tuple`.
129+ -- | Otherwise, the values stored in the `Both` are rewrapped into a `Tuple`.
122130fromThese :: forall a b . a -> b -> These a b -> Tuple a b
123131fromThese _ x (This a) = Tuple a x
124132fromThese a _ (That x) = Tuple a x
0 commit comments