Data.Pointed.Wedge
- Package
- purescript-pointed
- Repository
- vladciobanu/purescript-pointed
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.
#Wedge Source
data Wedge a bWedge a b can hold either no values, an a, or a b. The type is
isomorphic with Maybe (Either a b).
Constructors
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)(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 11Biapply WedgeBifoldable WedgeBitraversable 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)
#fromMaybeEither Source
fromMaybeEither :: forall b a. Maybe (Either a b) -> Wedge a bConstructs 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
#toMaybeEither Source
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)
#fromEitherMaybe Source
fromEitherMaybe :: forall b a. Either (Maybe a) (Maybe b) -> Wedge a bConstructs 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
#fromMaybeOne Source
fromMaybeOne :: forall b a. Maybe a -> Wedge a bConstructs a left-biased Wedge.
> fromMaybeOne Nothing :: Wedge String Int
Non
> fromMaybeOne (Just "hello") :: Wedge String Int
One "hello"
#fromMaybeEno Source
fromMaybeEno :: forall b a. Maybe b -> Wedge a bConstructs a right-biased Wedge.
> fromMaybeTwo Nothing :: Wedge String Int
Non
> fromMaybeTwo (Just 42) :: Wedge String Int
Eno 42
#wedge Source
wedge :: forall c b a. c -> (a -> c) -> (b -> c) -> Wedge a b -> cWedge 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)
#ones Source
ones :: forall f b a. Functor f => Foldable f => f (Wedge a b) -> List aTakes 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
#enos Source
enos :: forall f b a. Functor f => Foldable f => f (Wedge a b) -> List bTakes 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
#partition Source
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)
#partitionMap Source
partitionMap :: forall c b a t f. Alternative f => Traversable t => (a -> Wedge b c) -> t a -> Tuple (f b) (f c)Maps Wedges 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]
#distribute Source
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)
#codistribute Source
codistribute :: forall c b a. Either (Wedge a c) (Wedge b c) -> Wedge (Either a b) cCodistribute 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
The
Semigroupinstance may lose information (biased towardsEno):