Module

# Data.Pointed.Smash

Package
purescript-pointed
Repository

This module defines the `Smash` type, along with instances and functions that help use this type. Generally, `Smash` is used when the data is isomorphic to `Maybe (Tuple a b)`.

### #SmashSource

``data Smash a b``

`Smash` `a` `b` can hold either no values or an `a` and a `b`. The type is isomorphic with `Maybe (Tuple a b)`.

#### Constructors

• `Non`
• `Two a b`

#### Instances

• `(Eq a, Eq b) => Eq (Smash a b)`
• `(Ord a, Ord b) => Ord (Smash a b)`
• `Generic (Smash a b) _`
• `Functor (Smash a)`
• `(Semigroup a, Semigroup b) => Semigroup (Smash a b)`
• `(Semigroup a, Semigroup b) => Monoid (Smash a b)`
• `Bifunctor Smash`
``````> bimap show (_ + 10) (Non :: Smash Int Int)
Non

> bimap show (_ + 10) (Two 42 1 :: Smash Int Int)
Two "42" 11
``````
• `Biapply Smash`
• `Biapplicative Smash`
• `Bifoldable Smash`
• `Bitraversable Smash`
• `(Semigroup a) => Apply (Smash a)`
• `(Monoid a) => Applicative (Smash a)`
• `(Monoid a) => Bind (Smash a)`
• `(Monoid a) => Monad (Smash a)`
• `Foldable (Smash a)`
• `Traversable (Smash a)`

### #fromMaybeSource

``fromMaybe :: forall b a. Maybe (Tuple a b) -> Smash a b``

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

``````> fromMaybe Nothing :: Smash String Int
Non

> fromMaybe (Just (Tuple "hello" 42)) :: Smash String Int
Two "hello" 42
``````

### #toMaybeSource

``toMaybe :: forall b a. Smash a b -> Maybe (Tuple a b)``

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

``````> toMaybe Non :: Smash String Int
Nothing

> toMaybe (Two "hello" 42 :: Smash String Int)
Just (Tuple "hello" 42)
``````

### #smashSource

``smash :: forall c b a. c -> (a -> b -> c) -> Smash a b -> c``

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

For example, we can go from some `s :: Smash a b` to a `Maybe (Tuple a b)` using:

``````> smash Nothing (\a b -> Just (Tuple a b)) (Non :: Smash String Int)
Nothing

> smash Nothing (\a b -> Just (Tuple a b)) (Two "hello" 43 :: Smash String Int)
Just (Tuple "hello" 42)
``````

### #firstSource

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

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

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

> first (Two "hello" 42 :: Smash String Int)
Just "hello"
``````

### #secondSource

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

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

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

> second (Two "hello" 42 :: Smash String Int)
Just 42
``````

### #bothSource

``both :: forall b a. Smash a b -> Maybe (Tuple a b)``

Grabs an `a` and a `b` if they exist, otherwise returns nothing.

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

> second (Two "hello" 42 :: Smash String Int)
Just (Tuple "hello" 42)
``````

### #isNoneSource

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

Checks if the constructor is `Non`.

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

> isNone (Two "hello" 42 :: Smash String Int)
false
``````

### #isTwoSource

``isTwo :: forall b a. Smash a b -> Boolean``

Checks if the constructor is `Two`.

``````> isNone (Non :: Smash String Int)
false

> isNone (Two "hello" 42 :: Smash String Int)
true
``````

### #twosSource

``twos :: forall f b a. Functor f => Foldable f => f (Smash a b) -> List (Tuple a b)``

Takes all `a` and `b` values from a `Foldable` `Smash`.

``````> twos [Non :: Smash String Int]
Nil

> twos ([Two "hello" 42, Non, Two "world" 1] :: Array (Smash String Int))
Cons (Tuple "hello" 42) (Cons (Tuple "world" 1) Nil)
``````

### #currySource

``curry :: forall c b a. (Smash a b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c``

Expand a `Smash a b` to a `Maybe a` and a `Maybe b`. Note that the tuple `Tuple (Maybe a) (Maybe b)` has more information that we can store in a `Smash a b`.

``````Nothing, Nothing ~ Non
Just a , Nothing ~ Non
Nothing, Just b  ~ Non
Just a , Just b  ~ Two a b
``````

### #uncurrySource

``uncurry :: forall c b a. (Maybe a -> Maybe b -> Maybe c) -> Smash a b -> Maybe c``

Contract a `Smash a b` to a `Maybe a` and a `Maybe b`. Note that the tuple `Tuple (Maybe a) (Maybe b)` has more information that we can store in a `Smash a b`.

``````Non     ~ Nothing, Nothing
Two a b ~ Just a , Just b
``````

### #partitionSource

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

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

``````> partition [Non, Two "hello" 42, Two "world" 1] :: Tuple (List String) (List Int)
Tuple (Cons "hello" (Cons "world" Nil)) (Cons 42 (Cons 1 Nil))
``````

### #partitionMapSource

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

Maps `Smash`es over a `Traversable` and partitions the values by their position in the `Smash`.

``````> partitionMap (\i -> if i < 3 then Non else Two i (i*10)) [1, 2, 3, 4, 5] :: Tuple (Array Int) (Array Int)
Tuple [3, 4, 5] [30, 40, 50]
``````

### #distributeSource

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

Distribute `Smash` over a `Tuple`.

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

> distribute (Two (Tuple "hello" "world") 42 :: Smash (Tuple String String) Int)
Tuple (Two "hello" 42) (Two "world" 42)
``````

### #redistributeSource

``redistribute :: forall c b a. Tuple (Smash a c) (Smash b c) -> Smash (Tuple a b) c``

Distribute a `Tuple` over `Smash`.

``````> redistribute (Tuple Non Non :: Tuple (Smash String Int) (Smash String Int))
Non

> redistribute Tuple (Two "hello" 42) (Two "world" 42)
Two (Tuple "hello" "world") 42
``````

### #reassocLRSource

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

Reassociates a `Smash` from left to right. Note that `Non` and `Two Non c` both collapse to `Non`, so it is NOT the case that `reassocLR <<< reassocRL = identity`.

### #reassocRLSource

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

Reassociates a `Smash` from right to left. Note that `Non` and `Two Non c` both collapse to `Non`, so it is NOT the case that `reassocLR <<< reassocRL = identity`.

### #swapSource

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

Swap the inputs for a `Smash`.

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

> swap (Two "hello" 42)
Two 42 "hello"
``````