Data.Align

semialign-1.2.0.1: Align and Zip type-classes from the common Semialign ancestor.

Safe Haskell | Trustworthy
Language | Haskell2010

Data.Align

Contents

- Specialized aligns

Description

`These`-based aligning and unaligning of functors with non-uniform shapes.

For a traversals traversal of (bi)foldable (bi)functors through said functors see Data.Crosswalk.

Synopsis

- class Functor f => Semialign f where
  - align :: f a -> f b -> f (These a b)
  - alignWith :: (These a b -> c) -> f a -> f b -> f c
- class Semialign f => Align f where
  - nil :: f a
- class Semialign f => Unalign f where
  - unalign :: f (These a b) -> (f a, f b)
  - unalignWith :: (c -> These a b) -> f c -> (f a, f b)
- salign :: (Semialign f, Semigroup a) => f a -> f a -> f a
- padZip :: Semialign f => f a -> f b -> f (Maybe a, Maybe b)
- padZipWith :: Semialign f => (Maybe a -> Maybe b -> c) -> f a -> f b -> f c
- lpadZip :: [a] -> [b] -> [(Maybe a, b)]
- lpadZipWith :: (Maybe a -> b -> c) -> [a] -> [b] -> [c]
- rpadZip :: [a] -> [b] -> [(a, Maybe b)]
- rpadZipWith :: (a -> Maybe b -> c) -> [a] -> [b] -> [c]
- alignVectorWith :: (Vector v a, Vector v b, Vector v c) => (These a b -> c) -> v a -> v b -> v c

# Documentation

class Functor f => Semialign f where #

Functors supporting an `align` operation that takes the union of non-uniform shapes.

Minimal definition: either `align` or `alignWith`.

## Laws

The laws of `align` and `zip` resemble lattice laws. There is a plenty of laws, but they are simply satisfied.

And an addition property if `f` is `Foldable`, which tries to enforce `align`-feel: neither values are duplicated nor lost.

Note: ``join` f x = f x x`

Idempotency

    join align ≡ fmap (join These)

Commutativity

    align x y ≡ swap <$> align y x

Associativity

    align x (align y z) ≡ assoc <$> align (align x y) z

With

    alignWith f a b ≡ f <$> align a b

Functoriality

    align (f <$> x) (g <$> y) ≡ bimap f g <$> align x y

Alignedness, if `f` is `Foldable`

    toList x ≡ toListOf (folded . here) (align x y)
             ≡ mapMaybe justHere (toList (align x y))

And an addition property if `f` is `Foldable`, which tries to enforce `align`-feel: neither values are duplicated nor lost.

    toList x = toListOf (folded . here) (align x y)
             = mapMaybe justHere (toList (align x y))

Minimal complete definition

(align | alignWith)

Methods

align :: f a -> f b -> f (These a b) #

Analogous to ``zip``, combines two structures by taking the union of their shapes and using ``These`` to hold the elements.

alignWith :: (These a b -> c) -> f a -> f b -> f c #

Analogous to ``zipWith``, combines two structures by taking the union of their shapes and combining the elements with the given function.

#### Instances

Instances details

Semialign ZipList # | ``zipWith` = `liftA2`` .
Instance details Defined in Data.Semialign.Internal Methods align :: ZipList a -> ZipList b -> ZipList (These a b) # alignWith :: (These a b -> c) -> ZipList a -> ZipList b -> ZipList c #
Semialign Identity # |
Instance details Defined in Data.Semialign.Internal Methods align :: Identity a -> Identity b -> Identity (These a b) # alignWith :: (These a b -> c) -> Identity a -> Identity b -> Identity c #
Semialign IntMap # |
Instance details Defined in Data.Semialign.Internal Methods align :: IntMap a -> IntMap b -> IntMap (These a b) # alignWith :: (These a b -> c) -> IntMap a -> IntMap b -> IntMap c #
Semialign Seq # |
Instance details Defined in Data.Semialign.Internal Methods align :: Seq a -> Seq b -> Seq (These a b) # alignWith :: (These a b -> c) -> Seq a -> Seq b -> Seq c #
Semialign Tree # |
Instance details Defined in Data.Semialign.Internal Methods align :: Tree a -> Tree b -> Tree (These a b) # alignWith :: (These a b -> c) -> Tree a -> Tree b -> Tree c #
Semialign Vector # |
Instance details Defined in Data.Semialign.Internal Methods align :: Vector a -> Vector b -> Vector (These a b) # alignWith :: (These a b -> c) -> Vector a -> Vector b -> Vector c #
Semialign NonEmpty # |
Instance details Defined in Data.Semialign.Internal Methods align :: NonEmpty a -> NonEmpty b -> NonEmpty (These a b) # alignWith :: (These a b -> c) -> NonEmpty a -> NonEmpty b -> NonEmpty c #
Semialign Maybe # |
Instance details Defined in Data.Semialign.Internal Methods align :: Maybe a -> Maybe b -> Maybe (These a b) # alignWith :: (These a b -> c) -> Maybe a -> Maybe b -> Maybe c #
Semialign [] # |
Instance details Defined in Data.Semialign.Internal Methods align :: [a] -> [b] -> [These a b] # alignWith :: (These a b -> c) -> [a] -> [b] -> [c] #
Semialign (Proxy :: Type -> Type) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Proxy a -> Proxy b -> Proxy (These a b) # alignWith :: (These a b -> c) -> Proxy a -> Proxy b -> Proxy c #
Ord k => Semialign (Map k) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Map k a -> Map k b -> Map k (These a b) # alignWith :: (These a b -> c) -> Map k a -> Map k b -> Map k c #
(Eq k, Hashable k) => Semialign (HashMap k) # |
Instance details Defined in Data.Semialign.Internal Methods align :: HashMap k a -> HashMap k b -> HashMap k (These a b) # alignWith :: (These a b -> c) -> HashMap k a -> HashMap k b -> HashMap k c #
Monad m => Semialign (Stream m) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Stream m a -> Stream m b -> Stream m (These a b) # alignWith :: (These a b -> c) -> Stream m a -> Stream m b -> Stream m c #
Semialign (Tagged b) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Tagged b a -> Tagged b b0 -> Tagged b (These a b0) # alignWith :: (These a b0 -> c) -> Tagged b a -> Tagged b b0 -> Tagged b c #
Monad m => Semialign (Bundle m v) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Bundle m v a -> Bundle m v b -> Bundle m v (These a b) # alignWith :: (These a b -> c) -> Bundle m v a -> Bundle m v b -> Bundle m v c #
(Semialign f, Semialign g) => Semialign (Product f g) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Product f g a -> Product f g b -> Product f g (These a b) # alignWith :: (These a b -> c) -> Product f g a -> Product f g b -> Product f g c #
Semialign ((->) e) # |
Instance details Defined in Data.Semialign.Internal Methods align :: (e -> a) -> (e -> b) -> e -> These a b # alignWith :: (These a b -> c) -> (e -> a) -> (e -> b) -> e -> c #
(Semialign f, Semialign g) => Semialign (Compose f g) # |
Instance details Defined in Data.Semialign.Internal Methods align :: Compose f g a -> Compose f g b -> Compose f g (These a b) # alignWith :: (These a b -> c) -> Compose f g a -> Compose f g b -> Compose f g c #

class Semialign f => Align f where #

A unit of `align`.

## Laws

    (`align` nil) ≡ fmap This
    (nil `align`) ≡ fmap That

Methods

nil :: f a #

An empty structure. ``align``ing with ``nil`` will produce a structure with the same shape and elements as the other input, modulo ``This`` or ``That``.

#### Instances

Instances details

Align ZipList # |
Instance details Defined in Data.Semialign.Internal Methods nil :: ZipList a #
Align IntMap # |
Instance details Defined in Data.Semialign.Internal Methods nil :: IntMap a #
Align Seq # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Seq a #
Align Vector # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Vector a #
Align Maybe # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Maybe a #
Align [] # |
Instance details Defined in Data.Semialign.Internal Methods nil :: [a] #
Align (Proxy :: Type -> Type) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Proxy a #
Ord k => Align (Map k) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Map k a #
(Eq k, Hashable k) => Align (HashMap k) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: HashMap k a #
Monad m => Align (Stream m) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Stream m a #
Monad m => Align (Bundle m v) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Bundle m v a #
(Align f, Align g) => Align (Product f g) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Product f g a #
(Align f, Semialign g) => Align (Compose f g) # |
Instance details Defined in Data.Semialign.Internal Methods nil :: Compose f g a #

class Semialign f => Unalign f where #

Alignable functors supporting an "inverse" to `align`: splitting a union shape into its component parts.

## Laws

    uncurry align (unalign xs) ≡ xs
    unalign (align xs ys) ≡ (xs, ys)

## Compatibility note

In version 1 `unalign` was changed to return `(f a, f b)` pair, instead of `(f (Just a), f (Just b))`. Old behaviour can be achieved with if ever needed.

    >>> unzipWith (unalign . Just) [This 'a', That 'b', These 'c' 'd']
    ([Just 'a',Nothing,Just 'c'],[Nothing,Just 'b',Just 'd'])

Minimal complete definition

unalignWith | unalign

Methods

unalign :: f (These a b) -> (f a, f b) #

unalignWith :: (c -> These a b) -> f c -> (f a, f b) #

#### Instances

Instances details

Unalign IntMap # |
Instance details Defined in Data.Semialign.Internal Methods unalign :: IntMap (These a b) -> (IntMap a, IntMap b) # unalignWith :: (c -> These a b) -> IntMap c -> (IntMap a, IntMap b) #
Unalign Maybe # |
Instance details Defined in Data.Semialign.Internal Methods unalign :: Maybe (These a b) -> (Maybe a, Maybe b) # unalignWith :: (c -> These a b) -> Maybe c -> (Maybe a, Maybe b) #
Unalign (Proxy :: Type -> Type) # |
Instance details Defined in Data.Semialign.Internal Methods unalign :: Proxy (These a b) -> (Proxy a, Proxy b) # unalignWith :: (c -> These a b) -> Proxy c -> (Proxy a, Proxy b) #
Ord k => Unalign (Map k) # |
Instance details Defined in Data.Semialign.Internal Methods unalign :: Map k (These a b) -> (Map k a, Map k b) # unalignWith :: (c -> These a b) -> Map k c -> (Map k a, Map k b) #
(Eq k, Hashable k) => Unalign (HashMap k) # |
Instance details Defined in Data.Semialign.Internal Methods unalign :: HashMap k (These a b) -> (HashMap k a, HashMap k b) # unalignWith :: (c -> These a b) -> HashMap k c -> (HashMap k a, HashMap k b) #
(Unalign f, Unalign g) => Unalign (Product f g) # |
Instance details Defined in Data.Semialign.Internal Methods unalign :: Product f g (These a b) -> (Product f g a, Product f g b) # unalignWith :: (c -> These a b) -> Product f g c -> (Product f g a, Product f g b) #

# Specialized aligns

salign :: (Semialign f, Semigroup a) => f a -> f a -> f a #

Align two structures and combine with `<>`.

padZip :: Semialign f => f a -> f b -> f (Maybe a, Maybe b) #

Align two structures as in `zip`, but filling in blanks with `Nothing`.

padZipWith :: Semialign f => (Maybe a -> Maybe b -> c) -> f a -> f b -> f c #

Align two structures as in `zipWith`, but filling in blanks with `Nothing`.

lpadZip :: [a] -> [b] -> [(Maybe a, b)] #

Left-padded `zip`.

lpadZipWith :: (Maybe a -> b -> c) -> [a] -> [b] -> [c] #

Left-padded `zipWith`.

rpadZip :: [a] -> [b] -> [(a, Maybe b)] #

Right-padded `zip`.

rpadZipWith :: (a -> Maybe b -> c) -> [a] -> [b] -> [c] #

Right-padded `zipWith`.

alignVectorWith :: (Vector v a, Vector v b, Vector v c) => (These a b -> c) -> v a -> v b -> v c #