Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
The These
type and associated operations.
Synopsis
- data These a b
- these :: (a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c
- fromThese :: a -> b -> These a b -> (a, b)
- mergeThese :: (a -> a -> a) -> These a a -> a
- mergeTheseWith :: (a -> c) -> (b -> c) -> (c -> c -> c) -> These a b -> c
- partitionThese :: [These a b] -> ([a], [b], [(a, b)])
- partitionHereThere :: [These a b] -> ([a], [b])
- partitionEithersNE :: NonEmpty (Either a b) -> These (NonEmpty a) (NonEmpty b)
- distrThesePair :: These (a, b) c -> (These a c, These b c)
- undistrThesePair :: (These a c, These b c) -> These (a, b) c
- distrPairThese :: (These a b, c) -> These (a, c) (b, c)
- undistrPairThese :: These (a, c) (b, c) -> (These a b, c)
Documentation
The These
type represents values with two non-exclusive possibilities.
This can be useful to represent combinations of two values, where the
combination is defined if either input is. Algebraically, the type
represents These
A B(A + B + AB)
, which doesn't factor easily into
sums and products--a type like
is unclear and
awkward to use.Either
A (B, Maybe
A)
These
has straightforward instances of Functor
, Monad
, &c., and
behaves like a hybrid error/writer monad, as would be expected.
For zipping and unzipping of structures with These
values, see
Data.Align.
Instances
Functions to get rid of These
these :: (a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c Source #
Case analysis for the These
type.
mergeThese :: (a -> a -> a) -> These a a -> a Source #
Coalesce with the provided operation.
mergeTheseWith :: (a -> c) -> (b -> c) -> (c -> c -> c) -> These a b -> c Source #
bimap
and coalesce results with the provided operation.
Partition
partitionThese :: [These a b] -> ([a], [b], [(a, b)]) Source #
Select each constructor and partition them into separate lists.
partitionHereThere :: [These a b] -> ([a], [b]) Source #
Select here
and there
elements and partition them into separate lists.
Since: 0.8
partitionEithersNE :: NonEmpty (Either a b) -> These (NonEmpty a) (NonEmpty b) Source #
Like partitionEithers
but for NonEmpty
types.
Note: this is not online algorithm. In the worst case it will traverse the whole list before deciding the result constructor.
>>>
partitionEithersNE $ Left 'x' :| [Right 'y']
These ('x' :| "") ('y' :| "")
>>>
partitionEithersNE $ Left 'x' :| map Left "yz"
This ('x' :| "yz")
Since: 1.0.1
Distributivity
These distributivity combinators aren't isomorphisms!
distrPairThese :: (These a b, c) -> These (a, c) (b, c) Source #
undistrPairThese :: These (a, c) (b, c) -> (These a b, c) Source #