Module

Data.Pointed.Smash

Package
purescript-pointed
Repository
vladciobanu/purescript-pointed

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

#Smash Source

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

Instances

#fromMaybe Source

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

#toMaybe Source

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)

#smash Source

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)

#first Source

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"

#second Source

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

#both Source

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)

#isNone Source

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

#isTwo Source

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

#twos Source

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)

#curry Source

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

#uncurry Source

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

#partition Source

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

#partitionMap Source

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

Maps Smashes 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]

#distribute Source

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)

#redistribute Source

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

#reassocLR Source

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.

#reassocRL Source

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.

#swap Source

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"