Data.These

these-1.2: An either-or-both data type.

Safe Haskell | Safe
Language | Haskell2010

Data.These

Contents

- Functions to get rid of `These`
- Partition
- Distributivity

Description

The `These` type and associated operations. Now enhanced with Control.Lens magic!

Synopsis

- data These a b
  - = This a
  - | That b
  - | 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

data These a b #

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 ``These` A B` represents `(A + B + AB)`, which doesn't factor easily into sums and products--a type like ``Either` A (B, `Maybe` A)` is unclear and awkward to use.

`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.

Constructors

This a |
That b |
These a b |

#### Instances

Instances details

Assoc These # | Since: 0.8
Instance details Defined in Data.These Methods assoc :: These (These a b) c -> These a (These b c) # unassoc :: These a (These b c) -> These (These a b) c #
Swap These # | Since: 0.8
Instance details Defined in Data.These Methods swap :: These a b -> These b a #
Bifoldable These # |
Instance details Defined in Data.These Methods bifold :: Monoid m => These m m -> m # bifoldMap :: Monoid m => (a -> m) -> (b -> m) -> These a b -> m # bifoldr :: (a -> c -> c) -> (b -> c -> c) -> c -> These a b -> c # bifoldl :: (c -> a -> c) -> (c -> b -> c) -> c -> These a b -> c #
Bifoldable1 These # | Since: 1.2
Instance details Defined in Data.These Methods bifold1 :: Semigroup m => These m m -> m # bifoldMap1 :: Semigroup m => (a -> m) -> (b -> m) -> These a b -> m #
Bifunctor These # |
Instance details Defined in Data.These Methods bimap :: (a -> b) -> (c -> d) -> These a c -> These b d # first :: (a -> b) -> These a c -> These b c # second :: (b -> c) -> These a b -> These a c #
Bitraversable These # |
Instance details Defined in Data.These Methods bitraverse :: Applicative f => (a -> f c) -> (b -> f d) -> These a b -> f (These c d) #
Eq2 These # | Since: 1.1.1
Instance details Defined in Data.These Methods liftEq2 :: (a -> b -> Bool) -> (c -> d -> Bool) -> These a c -> These b d -> Bool #
Ord2 These # | Since: 1.1.1
Instance details Defined in Data.These Methods liftCompare2 :: (a -> b -> Ordering) -> (c -> d -> Ordering) -> These a c -> These b d -> Ordering #
Read2 These # | Since: 1.1.1
Instance details Defined in Data.These Methods liftReadsPrec2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> Int -> ReadS (These a b) # liftReadList2 :: (Int -> ReadS a) -> ReadS [a] -> (Int -> ReadS b) -> ReadS [b] -> ReadS [These a b] # liftReadPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec (These a b) # liftReadListPrec2 :: ReadPrec a -> ReadPrec [a] -> ReadPrec b -> ReadPrec [b] -> ReadPrec [These a b] #
Show2 These # | Since: 1.1.1
Instance details Defined in Data.These Methods liftShowsPrec2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> Int -> These a b -> ShowS # liftShowList2 :: (Int -> a -> ShowS) -> ([a] -> ShowS) -> (Int -> b -> ShowS) -> ([b] -> ShowS) -> [These a b] -> ShowS #
NFData2 These # | Since: 1.1.1
Instance details Defined in Data.These Methods liftRnf2 :: (a -> ()) -> (b -> ()) -> These a b -> () #
Hashable2 These # | Since: 1.1.1
Instance details Defined in Data.These Methods liftHashWithSalt2 :: (Int -> a -> Int) -> (Int -> b -> Int) -> Int -> These a b -> Int #
Generic1 (These a :: Type -> Type) # |
Instance details Defined in Data.These Associated Types
type Rep1 (These a :: Type -> Type) |
Instance details Defined in Data.These type Rep1 (These a :: Type -> Type) = D1 ('MetaData "These" "Data.These" "these-1.2-CD6Z5hIPDDoF4AOUmNwcyG" 'False) (C1 ('MetaCons "This" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: (C1 ('MetaCons "That" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1) :+: C1 ('MetaCons "These" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1)))
Methods from1 :: These a a0 -> Rep1 (These a) a0 # to1 :: Rep1 (These a) a0 -> These a a0 #
Foldable (These a) # |
Instance details Defined in Data.These Methods fold :: Monoid m => These a m -> m # foldMap :: Monoid m => (a0 -> m) -> These a a0 -> m # foldMap' :: Monoid m => (a0 -> m) -> These a a0 -> m # foldr :: (a0 -> b -> b) -> b -> These a a0 -> b # foldr' :: (a0 -> b -> b) -> b -> These a a0 -> b # foldl :: (b -> a0 -> b) -> b -> These a a0 -> b # foldl' :: (b -> a0 -> b) -> b -> These a a0 -> b # foldr1 :: (a0 -> a0 -> a0) -> These a a0 -> a0 # foldl1 :: (a0 -> a0 -> a0) -> These a a0 -> a0 # toList :: These a a0 -> [a0] # null :: These a a0 -> Bool # length :: These a a0 -> Int # elem :: Eq a0 => a0 -> These a a0 -> Bool # maximum :: Ord a0 => These a a0 -> a0 # minimum :: Ord a0 => These a a0 -> a0 # sum :: Num a0 => These a a0 -> a0 # product :: Num a0 => These a a0 -> a0 #
Eq a => Eq1 (These a) # | Since: 1.1.1
Instance details Defined in Data.These Methods liftEq :: (a0 -> b -> Bool) -> These a a0 -> These a b -> Bool #
Ord a => Ord1 (These a) # | Since: 1.1.1
Instance details Defined in Data.These Methods liftCompare :: (a0 -> b -> Ordering) -> These a a0 -> These a b -> Ordering #
Read a => Read1 (These a) # | Since: 1.1.1
Instance details Defined in Data.These Methods liftReadsPrec :: (Int -> ReadS a0) -> ReadS [a0] -> Int -> ReadS (These a a0) # liftReadList :: (Int -> ReadS a0) -> ReadS [a0] -> ReadS [These a a0] # liftReadPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec (These a a0) # liftReadListPrec :: ReadPrec a0 -> ReadPrec [a0] -> ReadPrec [These a a0] #
Show a => Show1 (These a) # | Since: 1.1.1
Instance details Defined in Data.These Methods liftShowsPrec :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> Int -> These a a0 -> ShowS # liftShowList :: (Int -> a0 -> ShowS) -> ([a0] -> ShowS) -> [These a a0] -> ShowS #
Traversable (These a) # |
Instance details Defined in Data.These Methods traverse :: Applicative f => (a0 -> f b) -> These a a0 -> f (These a b) # sequenceA :: Applicative f => These a (f a0) -> f (These a a0) # mapM :: Monad m => (a0 -> m b) -> These a a0 -> m (These a b) # sequence :: Monad m => These a (m a0) -> m (These a a0) #
Semigroup a => Applicative (These a) # |
Instance details Defined in Data.These Methods pure :: a0 -> These a a0 # (<*>) :: These a (a0 -> b) -> These a a0 -> These a b # liftA2 :: (a0 -> b -> c) -> These a a0 -> These a b -> These a c # (*>) :: These a a0 -> These a b -> These a b # (<*) :: These a a0 -> These a b -> These a a0 #
Functor (These a) # |
Instance details Defined in Data.These Methods fmap :: (a0 -> b) -> These a a0 -> These a b # (<$) :: a0 -> These a b -> These a a0 #
Semigroup a => Monad (These a) # |
Instance details Defined in Data.These Methods (>>=) :: These a a0 -> (a0 -> These a b) -> These a b # (>>) :: These a a0 -> These a b -> These a b # return :: a0 -> These a a0 #
NFData a => NFData1 (These a) # | Since: 1.1.1
Instance details Defined in Data.These Methods liftRnf :: (a0 -> ()) -> These a a0 -> () #
Hashable a => Hashable1 (These a) # | Since: 1.1.1
Instance details Defined in Data.These Methods liftHashWithSalt :: (Int -> a0 -> Int) -> Int -> These a a0 -> Int #
(Data a, Data b) => Data (These a b) # |
Instance details Defined in Data.These Methods gfoldl :: (forall d b0. Data d => c (d -> b0) -> d -> c b0) -> (forall g. g -> c g) -> These a b -> c (These a b) # gunfold :: (forall b0 r. Data b0 => c (b0 -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (These a b) # toConstr :: These a b -> Constr # dataTypeOf :: These a b -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (These a b)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (These a b)) # gmapT :: (forall b0. Data b0 => b0 -> b0) -> These a b -> These a b # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> These a b -> r # gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> These a b -> r # gmapQ :: (forall d. Data d => d -> u) -> These a b -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> These a b -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> These a b -> m (These a b) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> These a b -> m (These a b) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> These a b -> m (These a b) #
(Semigroup a, Semigroup b) => Semigroup (These a b) # |
Instance details Defined in Data.These Methods (<>) :: These a b -> These a b -> These a b # sconcat :: NonEmpty (These a b) -> These a b # stimes :: Integral b0 => b0 -> These a b -> These a b #
Generic (These a b) # |
Instance details Defined in Data.These Associated Types
type Rep (These a b) |
Instance details Defined in Data.These type Rep (These a b) = D1 ('MetaData "These" "Data.These" "these-1.2-CD6Z5hIPDDoF4AOUmNwcyG" 'False) (C1 ('MetaCons "This" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: (C1 ('MetaCons "That" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b)) :+: C1 ('MetaCons "These" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b))))
Methods from :: These a b -> Rep (These a b) x # to :: Rep (These a b) x -> These a b #
(Read a, Read b) => Read (These a b) # |
Instance details Defined in Data.These Methods readsPrec :: Int -> ReadS (These a b) # readList :: ReadS [These a b] # readPrec :: ReadPrec (These a b) # readListPrec :: ReadPrec [These a b] #
(Show a, Show b) => Show (These a b) # |
Instance details Defined in Data.These Methods showsPrec :: Int -> These a b -> ShowS # show :: These a b -> String # showList :: [These a b] -> ShowS #
(Binary a, Binary b) => Binary (These a b) # | Since: 0.7.1
Instance details Defined in Data.These Methods put :: These a b -> Put # get :: Get (These a b) # putList :: [These a b] -> Put #
(NFData a, NFData b) => NFData (These a b) # | Since: 0.7.1
Instance details Defined in Data.These Methods rnf :: These a b -> () #
(Eq a, Eq b) => Eq (These a b) # |
Instance details Defined in Data.These Methods (==) :: These a b -> These a b -> Bool # (/=) :: These a b -> These a b -> Bool #
(Ord a, Ord b) => Ord (These a b) # |
Instance details Defined in Data.These Methods compare :: These a b -> These a b -> Ordering # (<) :: These a b -> These a b -> Bool # (<=) :: These a b -> These a b -> Bool # (>) :: These a b -> These a b -> Bool # (>=) :: These a b -> These a b -> Bool # max :: These a b -> These a b -> These a b # min :: These a b -> These a b -> These a b #
(Hashable a, Hashable b) => Hashable (These a b) # |
Instance details Defined in Data.These Methods hashWithSalt :: Int -> These a b -> Int # hash :: These a b -> Int #
type Rep1 (These a :: Type -> Type) # |
Instance details Defined in Data.These type Rep1 (These a :: Type -> Type) = D1 ('MetaData "These" "Data.These" "these-1.2-CD6Z5hIPDDoF4AOUmNwcyG" 'False) (C1 ('MetaCons "This" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: (C1 ('MetaCons "That" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1) :+: C1 ('MetaCons "These" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1)))
type Rep (These a b) # |
Instance details Defined in Data.These type Rep (These a b) = D1 ('MetaData "These" "Data.These" "these-1.2-CD6Z5hIPDDoF4AOUmNwcyG" 'False) (C1 ('MetaCons "This" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)) :+: (C1 ('MetaCons "That" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b)) :+: C1 ('MetaCons "These" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 b))))

# Functions to get rid of `These`

these :: (a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c #

Case analysis for the `These` type.

fromThese :: a -> b -> These a b -> (a, b) #

Takes two default values and produces a tuple.

mergeThese :: (a -> a -> a) -> These a a -> a #

Coalesce with the provided operation.

mergeTheseWith :: (a -> c) -> (b -> c) -> (c -> c -> c) -> These a b -> c #

`bimap` and coalesce results with the provided operation.

# Partition

partitionThese :: [These a b] -> ([a], [b], [(a, b)]) #

Select each constructor and partition them into separate lists.

partitionHereThere :: [These a b] -> ([a], [b]) #

Select `here` and `there` elements and partition them into separate lists.

Since: 0.8

partitionEithersNE :: NonEmpty (Either a b) -> These (NonEmpty a) (NonEmpty b) #

Like `partitionEithers` but for `NonEmpty` types.

- either all are `Left`
- either all are `Right`
- or there is both `Left` and `Right` stuff

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!

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) #