| Safe Haskell | Trustworthy |
|---|---|
| Language | Haskell2010 |
Data.Zip
Description
Zipping and unzipping of functors with non-uniform shapes.
Synopsis
- class Functor f => Semialign (f :: Type -> Type) where
- class Semialign f => Zip (f :: Type -> Type) where
- class Zip f => Repeat (f :: Type -> Type) where
- repeat :: a -> f a
- class Zip f => Unzip (f :: Type -> Type) where
- unzipDefault :: Functor f => f (a, b) -> (f a, f b)
- newtype Zippy (f :: Type -> Type) a = Zippy {
- getZippy :: f a
Documentation
class Functor f => Semialign (f :: Type -> Type) 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))
Methods
align :: f a -> f b -> f (These a b) #
Analogous to , combines two structures by taking the union of
their shapes and using zip to hold the elements.These
alignWith :: (These a b -> c) -> f a -> f b -> f c #
Analogous to , combines two structures by taking the union of
their shapes and combining the elements with the given function.zipWith
Instances
| Semialign ZipList # |
|
| Semialign Identity # | |
| Semialign NonEmpty # | |
| Semialign IntMap # | |
| Semialign Seq # | |
| Semialign Tree # | |
| Semialign Vector # | |
| Semialign Maybe # | |
| Semialign [] # | |
| Semialign (Proxy :: Type -> Type) # | |
| Ord k => Semialign (Map k) # | |
| (Eq k, Hashable k) => Semialign (HashMap k) # | |
| Monad m => Semialign (Stream m) # | |
| Semialign (Tagged b) # | |
| Monad m => Semialign (Bundle m v) # | |
| (Semialign f, Semialign g) => Semialign (Product f g) # | |
| Semialign ((->) e) # | |
| (Semialign f, Semialign g) => Semialign (Compose f g) # | |
class Semialign f => Zip (f :: Type -> Type) where #
Functors supporting a zip operation that takes the intersection of
non-uniform shapes.
Minimal definition: either zip or zipWith.
Idempotency
join zip ≡ fmap (join (,))
Commutativity
zip x y ≡ swap <$> zip y x
Associativity
zip x (zip y z) ≡ assoc <$> zip (zip x y) z
Absorption
fst <$> zip xs (align xs ys) ≡ xs
toThis <$> align xs (zip xs ys) ≡ This <$> xs
where
toThis (This a) = This a
toThis (These a _) = This a
toThis (That b) = That b
With
zipWith f a b ≡ f <$> zip a b
Functoriality
zip (f <$> x) (g <$> y) ≡ bimap f g <$> zip x y
Zippyness
fmap fst (zip x x) ≡ x fmap snd (zip x x) ≡ x zip (fmap fst x) (fmap snd x) ≡ x
Distributivity
align (zip xs ys) zs ≡ undistrThesePair <$> zip (align xs zs) (align ys zs)
distrPairThese <$> zip (align xs ys) zs ≡ align (zip xs zs) (zip ys zs)
zip (align xs ys) zs ≡ undistrPairThese <$> align (zip xs zs) (zip ys zs)
Note, the following doesn't hold:
distrThesePair <$> align (zip xs ys) zs ≢ zip (align xs zs) (align ys zs)
when xs = [] and ys = zs = [0], then
the left hand side is "only" [(,
but the right hand side is That 0, That 0)][(.That 0, These 0 0)]
Methods
zip :: f a -> f b -> f (a, b) #
Combines two structures by taking the intersection of their shapes and using pair to hold the elements.
zipWith :: (a -> b -> c) -> f a -> f b -> f c #
Combines two structures by taking the intersection of their shapes and combining the elements with the given function.
Instances
| Zip ZipList # | |
| Zip Identity # | |
| Zip NonEmpty # | |
| Zip IntMap # | |
| Zip Seq # | |
| Zip Tree # | |
| Zip Vector # | |
| Zip Maybe # | |
| Zip [] # | |
Defined in Data.Semialign.Internal | |
| Zip (Proxy :: Type -> Type) # | |
| Ord k => Zip (Map k) # | |
| (Eq k, Hashable k) => Zip (HashMap k) # | |
| Monad m => Zip (Stream m) # | |
| Zip (Tagged b) # | |
| Monad m => Zip (Bundle m v) # | |
| (Zip f, Zip g) => Zip (Product f g) # | |
| Zip ((->) e) # | |
Defined in Data.Semialign.Internal | |
| (Zip f, Zip g) => Zip (Compose f g) # | |
class Zip f => Repeat (f :: Type -> Type) where #
Zippable functors supporting left and right units
Unit
fst <$> zip xs (repeat y) ≡ xs snd <$> zip (repeat x) ys ≡ ys
Instances
| Repeat ZipList # | |
Defined in Data.Semialign.Internal | |
| Repeat Identity # | |
Defined in Data.Semialign.Internal | |
| Repeat NonEmpty # | |
Defined in Data.Semialign.Internal | |
| Repeat Tree # | |
Defined in Data.Semialign.Internal | |
| Repeat Maybe # | |
Defined in Data.Semialign.Internal | |
| Repeat [] # | |
Defined in Data.Semialign.Internal | |
| Repeat (Proxy :: Type -> Type) # | |
Defined in Data.Semialign.Internal | |
| Repeat (Tagged b) # | |
Defined in Data.Semialign.Internal | |
| (Repeat f, Repeat g) => Repeat (Product f g) # | |
Defined in Data.Semialign.Internal | |
| Repeat ((->) e) # | |
Defined in Data.Semialign.Internal | |
| (Repeat f, Repeat g) => Repeat (Compose f g) # | |
Defined in Data.Semialign.Internal | |
class Zip f => Unzip (f :: Type -> Type) where #
Right inverse of zip.
This class is definable for every Functor. See unzipDefault.
Laws
uncurry zip (unzip xs) ≡ xs unzip (zip xs xs) ≡ (xs, xs)
Note:
unzip (zip xs ys) ≢ (xs, _) or (_, ys)
For sequence-like types this holds, but for Map-like it doesn't.
Instances
| Unzip ZipList # | |
| Unzip Identity # | |
| Unzip NonEmpty # | |
| Unzip IntMap # | |
| Unzip Seq # | |
| Unzip Tree # | |
| Unzip Vector # | |
| Unzip Maybe # | |
| Unzip [] # | |
Defined in Data.Semialign.Internal | |
| Unzip (Proxy :: Type -> Type) # | |
| Ord k => Unzip (Map k) # | |
| (Eq k, Hashable k) => Unzip (HashMap k) # | |
| Unzip (Tagged b) # | |
| (Unzip f, Unzip g) => Unzip (Product f g) # | |
| (Unzip f, Unzip g) => Unzip (Compose f g) # | |
unzipDefault :: Functor f => f (a, b) -> (f a, f b) #
newtype Zippy (f :: Type -> Type) a #
Instances
| Repeat f => Applicative (Zippy f) # | |
| Functor f => Functor (Zippy f) # | |
| Zip f => Apply (Zippy f) # | |
| (Repeat f, Monoid a) => Monoid (Zippy f a) # | |
| (Zip f, Semigroup a) => Semigroup (Zippy f a) # | |
| Read (f a) => Read (Zippy f a) # | |
| Show (f a) => Show (Zippy f a) # | |
| Eq (f a) => Eq (Zippy f a) # | |
| Ord (f a) => Ord (Zippy f a) # | |