Module

# Data.Pointed.Wedge

Package
purescript-pointed
Repository

This module defines the `Wedge` type, along with instances and functions that help use this type. Generally, `Wedge` is used when the data is more vague than simply using `Either a b`, i.e. when either or both `a` and `b` might be missing.

### #WedgeSource

``data Wedge a b``

`Wedge` `a` `b` can hold either no values, an `a`, or a `b`. The type is isomorphic with `Maybe (Either a b)`.

#### Constructors

• `Non`
• `One a`
• `Eno b`

#### Instances

• `(Eq a, Eq b) => Eq (Wedge a b)`
• `(Ord a, Ord b) => Ord (Wedge a b)`
• `Generic (Wedge a b) _`
• `Functor (Wedge a)`
• `(Semigroup a, Semigroup b) => Semigroup (Wedge a b)`

The `Semigroup` instance may lose information (biased towards `Eno`):

``````> (One "hello" :: Wedge String Int) <> (Eno 42 :: Wedge String Int)
Eno 42
``````
• `(Semigroup a, Semigroup b) => Monoid (Wedge a b)`
• `Bifunctor Wedge`
``````> bimap show (_ + 10) (Non :: Wedge Int Int)
Non

> bimap show (_ + 10) (One 42 :: Wedge Int Int)
One "42"

> bimap show (_ + 10) (Eno 1 :: Wedge Int Int)
One 11
``````
• `Biapply Wedge`
• `Bifoldable Wedge`
• `Bitraversable Wedge`
• `(Semigroup a) => Apply (Wedge a)`
• `(Monoid a) => Applicative (Wedge a)`
• `(Monoid a) => Bind (Wedge a)`
• `(Monoid a) => Monad (Wedge a)`
• `Foldable (Wedge a)`
• `Traversable (Wedge a)`

### #fromMaybeEitherSource

``fromMaybeEither :: forall b a. Maybe (Either a b) -> Wedge a b``

Constructs a `Wedge` given an isomorphic representation. Note that this is the precise inverse of `toMaybeEither`.

``````> fromMaybeEither Nothing :: Wedge String Int
Non

> fromMaybeEither (Just (Left "hello")) :: Wedge String Int
One "hello"

> fromMaybeEither (Just (Right 42)) :: Wedge String Int
Eno 42
``````

### #toMaybeEitherSource

``toMaybeEither :: forall b a. Wedge a b -> Maybe (Either a b)``

Destructs a `Wedge` from an isomorphic representation. Note that this is the precise inverse of `fromMaybeEither`.

``````> toMaybeEither (Non :: Wedge String Int)
Nothing

> toMaybeEither (One "hello" :: Wedge String Int)
Just (Left "hello")

> toMaybeEither (Eno 42 :: Wedge String Int)
Just (Right 42)
``````

### #fromEitherMaybeSource

``fromEitherMaybe :: forall b a. Either (Maybe a) (Maybe b) -> Wedge a b``

Constructs a `Wedge` given an either of maybes. Note that this is the precise inverse of `toMaybeEither`.

``````> fromEitherMaybe (Left Nothing) :: Wedge String Int
Non

> fromEitherMaybe (Left (Just "hello")) :: Wedge String Int
One "hello"

> fromEitherMaybe (Right Nothing) :: Wedge String Int
Non

> fromEitherMaybe (Right (Just 42)) :: Wedge String Int
Eno 42
``````

### #fromMaybeOneSource

``fromMaybeOne :: forall b a. Maybe a -> Wedge a b``

Constructs a left-biased `Wedge`.

``````> fromMaybeOne Nothing :: Wedge String Int
Non

> fromMaybeOne (Just "hello") :: Wedge String Int
One "hello"
``````

### #fromMaybeEnoSource

``fromMaybeEno :: forall b a. Maybe b -> Wedge a b``

Constructs a right-biased `Wedge`.

``````> fromMaybeTwo Nothing :: Wedge String Int
Non

> fromMaybeTwo (Just 42) :: Wedge String Int
Eno 42
``````

### #wedgeSource

``wedge :: forall c b a. c -> (a -> c) -> (b -> c) -> Wedge a b -> c``

`Wedge` catamorphism (fold). Takes an input for each possible constructor and translates it to `c`.

For example, we can go from some `w :: Wedge a b` to a `Maybe (Either a b)` using:

``````> wedge Nothing Left Right (Non :: Wedge String Int)
Non

> wedge Nothing Left Right (One "hello" :: Wedge String Int)
Just (Left "hello")

> wedge Nothing Left Right (Eno 42 :: Wedge String Int)
Just (Right 42)
``````

### #firstSource

``first :: forall b a. Wedge a b -> Maybe a``

Grabs an `a` if it exists, otherwise returns nothing.

``````> first (Non :: Wedge String Int)
Nothing

> first (One "hello" :: Wedge String Int)
Just "hello"

> first (Eno 42 :: Wedge String Int)
Nothing
``````

### #secondSource

``second :: forall b a. Wedge a b -> Maybe b``

Grabs a `b` if it exists, otherwise returns nothing.

``````> second (Non :: Wedge String Int)
Nothing

> second (One "hello" :: Wedge String Int)
Nothing

> second (Eno 42 :: Wedge String Int)
Just 42
``````

### #isNoneSource

``isNone :: forall b a. Wedge a b -> Boolean``

Checks if the constructor is `Non`.

``````> isNone (Non :: Wedge String Int)
true

> isNone (One "hello" :: Wedge String Int)
false

> isNone (Eno 42 :: Wedge String Int)
false
``````

### #isOneSource

``isOne :: forall b a. Wedge a b -> Boolean``

Checks if the constructor is `One`.

``````> isOne (Non :: Wedge String Int)
false

> isOne (One "hello" :: Wedge String Int)
true

> isOne (Eno 42 :: Wedge String Int)
false
``````

### #isEnoSource

``isEno :: forall b a. Wedge a b -> Boolean``

Checks if the constructor is `Eno`.

``````> isEno (Non :: Wedge String Int)
false

> isEno (One "hello" :: Wedge String Int)
false

> isEno (Eno 42 :: Wedge String Int)
true
``````

### #onesSource

``ones :: forall f b a. Functor f => Foldable f => f (Wedge a b) -> List a``

Takes all `a` values from a `Foldable` `Wedge`.

``````> ones [Non :: Wedge String Int]
Nil

> ones ([One "hello", One "world"] :: Array (Wedge String Int))
Cons "hello" (Cons "world" Nil)

> ones ([Eno 42, Eno 13] :: Array (Wedge String Int))
Nil

> ones ([One "hello", Eno 42] :: Array (Wedge String Int))
Cons "hello" Nil
``````

### #enosSource

``enos :: forall f b a. Functor f => Foldable f => f (Wedge a b) -> List b``

Takes all `b` values from a `Foldable` `Wedge`.

``````> enos [Non :: Wedge String Int]
Nil

> enos ([One "hello", One "world"] :: Array (Wedge String Int))
Nil

> enos ([Eno 42, Eno 13] :: Array (Wedge String Int))
Cons 42 (Cons 13 Nil)

> enos ([One "hello", Eno 42] :: Wedge String Int)
Cons 42 Nil
``````

### #partitionSource

``partition :: forall b a t f. Foldable t => Alternative f => t (Wedge a b) -> Tuple (f a) (f b)``

Grab all `a` and `b` values from a `Foldable` `Wedge a b` and combine them each into an `Alternative` `f` tuple.

``````> partition [Non, One "hello", Eno 42] :: Tuple (List String) (List Int)
Tuple (Cons "hello" Nil) (Cons 42)
``````

### #partitionMapSource

``partitionMap :: forall c b a t f. Alternative f => Traversable t => (a -> Wedge b c) -> t a -> Tuple (f b) (f c)``

Maps `Wedge`s over a `Traversable` and partitions the values by their position in the `Wedge`.

``````> partitionMap (\i -> if i < 3 then One i else Eno i) [1, 2, 3, 4, 5] :: Tuple (Array Int) (Array Int)
Tuple [1, 2] [3, 4, 5]
``````

### #distributeSource

``distribute :: forall c b a. Wedge (Tuple a b) c -> Tuple (Wedge a c) (Wedge b c)``

Distribute `Wedge` over a `Tuple`.

``````> distribute (Non :: Wedge (Tuple String String) Int)
Tuple Non Non

> distribute (One (Tuple "hello" "world") :: Wedge (Tuple String String) Int)
Tuple (One "hello") (One "world")

> distribute (Eno 42 :: Wedge (Tuple String String) Int)
Tuple (Eno 42) (Eno 42)
``````

### #codistributeSource

``codistribute :: forall c b a. Either (Wedge a c) (Wedge b c) -> Wedge (Either a b) c``

Codistribute `Either` over a `Wedge`.

``````> codistribute (Left Non :: Either (Wedge String Int) (Wedge Boolean Int))
Non

> codistribute (Left (One "hello") :: Either (Wedge String Int) (Wedge Boolean Int))
One (Left "hello")

> codistribute (Left (Eno 42) :: Either (Wedge String Int) (Wedge Boolean Int))
Eno 42

> codistribute (Right Non :: Either (Wedge String Int) (Wedge Boolean Int))
Non

> codistribute (Right (One true) :: Either (Wedge String Int) (Wedge Boolean Int))
One (Right true)

> codistribute (Right (Eno 42) :: Either (Wedge String Int) (Wedge Boolean Int))
Eno 42
``````

### #reassocLRSource

``reassocLR :: forall c b a. Wedge (Wedge a b) c -> Wedge a (Wedge b c)``

Reassociates a `Wedge` from left to right. Note that this is the inverse of `reassocRL`, i.e.: `reassocRL <<< reassocLR = identity`.

### #reassocRLSource

``reassocRL :: forall c b a. Wedge a (Wedge b c) -> Wedge (Wedge a b) c``

Reassociates a `Wedge` from right to left. Note that this is the inverse of `reassocLR`, i.e.: `reassocRL <<< reassocLR = identity`.

### #swapSource

``swap :: forall b a. Wedge a b -> Wedge b a``

Swap the inputs for a `Wedge`.

``````> swap (Non :: Wedge String Int)
Non

> swap (One "hello" :: Wedge String Int)
Eno "hello"

> swap (Eno 42 :: Wedge String Int)
One 42
``````