Module

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 b

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

Constructors

Instances

#fromMaybeEither Source

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

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

#fromMaybeOne Source

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"

#fromMaybeEno Source

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

#wedge Source

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)

#first Source

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

#second Source

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

#isNone Source

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

#isOne Source

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

#isEno Source

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

#ones Source

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

#enos Source

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

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

#reassocLR Source

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.

#reassocRL Source

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.

#swap Source

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