@@ -10,6 +10,8 @@ import Data.Maybe (Maybe(..), isJust)
10
10
import Data.Traversable (class Traversable , class Foldable , foldMap , foldl , foldr )
11
11
import Data.Tuple (Tuple (..))
12
12
13
+ -- | Data type isomorphic to `α ∨ β ∨ (α ∧ β)` or
14
+ -- | `Either a (Either b (Tuple a b))`.
13
15
data These a b
14
16
= This a
15
17
| That b
@@ -98,6 +100,8 @@ instance showThese :: (Show a, Show b) => Show (These a b) where
98
100
show (That y) = " (That " <> show y <> " )"
99
101
show (Both x y) = " (Both " <> show x <> " " <> show y <> " )"
100
102
103
+ -- | Given functions to handle each constructor, collapse a `These` value
104
+ -- | into single value.
101
105
these :: forall a b c . (a -> c ) -> (b -> c ) -> (a -> b -> c ) -> These a b -> c
102
106
these l _ _ (This a) = l a
103
107
these _ r _ (That x) = r x
@@ -119,6 +123,10 @@ maybeThese = case _, _ of
119
123
Just a, Just b -> Just (Both a b)
120
124
Nothing , Nothing -> Nothing
121
125
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`.
122
130
fromThese :: forall a b . a -> b -> These a b -> Tuple a b
123
131
fromThese _ x (This a) = Tuple a x
124
132
fromThese a _ (That x) = Tuple a x
0 commit comments