Safe Haskell | None |
---|---|
Language | Haskell2010 |
These
-based zipping and unzipping of functors with non-uniform
shapes, plus traversal of (bi)foldable (bi)functors through said
functors.
Synopsis
- class Functor f => Semialign f where
- class Semialign f => Align f where
- nil :: f a
- malign :: (Align f, Monoid a) => f a -> f a -> f a
- salign :: (Align f, Semigroup a) => f a -> f a -> f a
- padZip :: Align f => f a -> f b -> f (Maybe a, Maybe b)
- padZipWith :: Align 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
- class Align f => Unalign f where
- class (Functor t, Foldable t) => Crosswalk t where
- class (Bifunctor t, Bifoldable t) => Bicrosswalk t where
- bicrosswalk :: Align f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d)
- bisequenceL :: Align f => t (f a) (f b) -> f (t a b)
Documentation
class Functor f => Semialign f where Source #
Functors supporting a zip operation that takes the union of non-uniform shapes.
If your functor is actually a functor from Kleisli Maybe
to
Hask
(so it supports maybeMap :: (a -> Maybe b) -> f a -> f
b
), then an Align
instance is making your functor lax monoidal
w.r.t. the cartesian monoidal structure on Kleisli Maybe
,
because These
is the cartesian product in that category (a ->
Maybe (These b c) ~ (a -> Maybe b, a -> Maybe c))
. This insight
is due to rwbarton.
Minimal definition: either align
or alignWith
.
Laws:
join align = fmap (join These) align (f <$> x) (g <$> y) = bimap f g <$> align x y alignWith f a b = f <$> align a b align x (align y z) = fmap assoc (align (align x y) z)
Note: join
f x = f x x
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))
align :: f a -> f b -> f (These a b) Source #
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 Source #
Analogous to
, combines two structures by taking the union of
their shapes and combining the elements with the given function.zipWith
Instances
Semialign [] Source # | |
Semialign Maybe Source # | |
Semialign ZipList Source # | |
Semialign Identity Source # | |
Semialign NonEmpty Source # | |
Semialign IntMap Source # | |
Semialign Seq Source # | |
Semialign Vector Source # | |
(Eq k, Hashable k) => Semialign (HashMap k) Source # | |
Ord k => Semialign (Map k) Source # | |
Monad m => Semialign (Stream m) Source # | |
Monad m => Semialign (Bundle m v) Source # | |
(Semialign f, Semialign g) => Semialign (Product f g) Source # | |
class Semialign f => Align f where Source #
Instances
Align [] Source # | |
Defined in Data.Align | |
Align Maybe Source # | |
Defined in Data.Align | |
Align ZipList Source # | |
Defined in Data.Align | |
Align IntMap Source # | |
Defined in Data.Align | |
Align Seq Source # | |
Defined in Data.Align | |
Align Vector Source # | |
Defined in Data.Align | |
(Eq k, Hashable k) => Align (HashMap k) Source # | |
Defined in Data.Align | |
Ord k => Align (Map k) Source # | |
Defined in Data.Align | |
Monad m => Align (Stream m) Source # | |
Defined in Data.Align | |
Monad m => Align (Bundle m v) Source # | |
Defined in Data.Align | |
(Align f, Align g) => Align (Product f g) Source # | |
Defined in Data.Align |
Specialized aligns
salign :: (Align f, Semigroup a) => f a -> f a -> f a Source #
Align two structures and combine with <>
.
Since: 0.7.3
lpadZipWith :: (Maybe a -> b -> c) -> [a] -> [b] -> [c] Source #
Left-padded zipWith
.
rpadZipWith :: (a -> Maybe b -> c) -> [a] -> [b] -> [c] Source #
Right-padded zipWith
.
alignVectorWith :: (Vector v a, Vector v b, Vector v c) => (These a b -> c) -> v a -> v b -> v c Source #
Unalign
class Align f => Unalign f where Source #
Alignable functors supporting an "inverse" to align
: splitting
a union shape into its component parts.
Minimal definition: nothing; a default definition is provided, but it may not have the desired definition for all functors. See the source for more information.
Laws:
unalign nil = (nil, nil) unalign (This <$> x) = (Just <$> x, Nothing <$ x) unalign (That <$> y) = (Nothing <$ y, Just <$> y) unalign (join These <$> x) = (Just <$> x, Just <$> x) unalign ((x `These`) <$> y) = (Just x <$ y, Just <$> y) unalign ((`These` y) <$> x) = (Just <$> x, Just y <$ x)
Nothing
Crosswalk
class (Functor t, Foldable t) => Crosswalk t where Source #
Foldable functors supporting traversal through an alignable functor.
Minimal definition: crosswalk
or sequenceL
.
Laws:
crosswalk (const nil) = const nil crosswalk f = sequenceL . fmap f
Bicrosswalk
class (Bifunctor t, Bifoldable t) => Bicrosswalk t where Source #
Bifoldable bifunctors supporting traversal through an alignable functor.
Minimal definition: bicrosswalk
or bisequenceL
.
Laws:
bicrosswalk (const empty) (const empty) = const empty bicrosswalk f g = bisequenceL . bimap f g
bicrosswalk :: Align f => (a -> f c) -> (b -> f d) -> t a b -> f (t c d) Source #
bisequenceL :: Align f => t (f a) (f b) -> f (t a b) Source #
Instances
Bicrosswalk Either Source # | |
Defined in Data.Align | |
Bicrosswalk These Source # | |
Defined in Data.Align |