1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 23:11:50 +03:00

🔥 Crosswalk.

This commit is contained in:
Rob Rix 2016-03-15 08:22:55 -04:00
parent 70be591e3f
commit a22f713a97

View File

@ -38,24 +38,6 @@ instance Align Maybe where
| otherwise = Nothing | otherwise = Nothing
-- | A functor which can be traversed through an `Align`able functor, inverting the nesting of one in the other.
-- |
-- | Analogous with `zip`, in that it can e.g. turn a tuple of lists into a list of tuples.
class Functor t => Crosswalk t where
-- | Embed a structure into an `Align`able functor by mapping its elements into that functor and convoluting (inverting the embedding).
crosswalk :: Align f => (a -> f b) -> t a -> f (t b)
crosswalk f = sequenceL . fmap f
-- | Convolute (invert the embedding of) a structure over an `Align`able functor.
sequenceL :: Align f => t (f a) -> f (t a)
sequenceL = crosswalk id
instance Crosswalk Identity where
crosswalk f = fmap Identity . f . runIdentity
instance Crosswalk Maybe where
crosswalk f = maybe nil (fmap Just) . fmap f
-- | A functor which can be traversed through an `Align`able functor, inverting the nesting of one in the other, given some default value. -- | A functor which can be traversed through an `Align`able functor, inverting the nesting of one in the other, given some default value.
-- | -- |
-- | Analogous with `zip`, in that it can e.g. turn a tuple of lists into a list of tuples. -- | Analogous with `zip`, in that it can e.g. turn a tuple of lists into a list of tuples.
@ -69,4 +51,4 @@ class Functor t => TotalCrosswalk t where
tsequenceL d = tcrosswalk d id tsequenceL d = tcrosswalk d id
instance TotalCrosswalk Identity where instance TotalCrosswalk Identity where
tcrosswalk _ = crosswalk tcrosswalk _ f = fmap Identity . f . runIdentity