From 1d7645cafe955972c3a3d54a9b5cdeff029373a4 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Fri, 22 Jul 2016 17:49:07 -0400 Subject: [PATCH] GMergeable has a gmerge method generalizing gsequenceAlt. --- src/Data/Mergeable.hs | 2 +- src/Data/Mergeable/Generic.hs | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Data/Mergeable.hs b/src/Data/Mergeable.hs index 283cd2609..5c48c888c 100644 --- a/src/Data/Mergeable.hs +++ b/src/Data/Mergeable.hs @@ -15,4 +15,4 @@ class Functor t => Mergeable t where -- Instances -instance Mergeable [] where sequenceAlt = gsequenceAlt +instance Mergeable [] where sequenceAlt = gmerge identity diff --git a/src/Data/Mergeable/Generic.hs b/src/Data/Mergeable/Generic.hs index 2d4b6e504..25637d2ef 100644 --- a/src/Data/Mergeable/Generic.hs +++ b/src/Data/Mergeable/Generic.hs @@ -7,36 +7,36 @@ import Prologue -- Classes class GMergeable t where - gsequenceAlt :: Alternative f => t (f a) -> f (t a) + gmerge :: Alternative f => (a -> f b) -> t a -> f (t b) genericSequenceAlt :: (Generic1 t, GMergeable (Rep1 t), Alternative f) => t (f a) -> f (t a) -genericSequenceAlt = fmap to1 . gsequenceAlt . from1 +genericSequenceAlt = fmap to1 . gmerge identity . from1 -- Instances instance GMergeable U1 where - gsequenceAlt _ = pure U1 + gmerge _ _ = pure U1 instance GMergeable Par1 where - gsequenceAlt (Par1 a) = Par1 <$> a + gmerge f (Par1 a) = Par1 <$> f a instance GMergeable (K1 i c) where - gsequenceAlt (K1 a) = pure (K1 a) + gmerge _ (K1 a) = pure (K1 a) instance GMergeable f => GMergeable (Rec1 f) where - gsequenceAlt (Rec1 a) = Rec1 <$> gsequenceAlt a + gmerge f (Rec1 a) = Rec1 <$> gmerge f a instance GMergeable f => GMergeable (M1 i c f) where - gsequenceAlt (M1 a) = M1 <$> gsequenceAlt a + gmerge f (M1 a) = M1 <$> gmerge f a instance (GMergeable f, GMergeable g) => GMergeable (f :+: g) where - gsequenceAlt (L1 a) = L1 <$> gsequenceAlt a - gsequenceAlt (R1 b) = R1 <$> gsequenceAlt b + gmerge f (L1 a) = L1 <$> gmerge f a + gmerge f (R1 b) = R1 <$> gmerge f b instance (GMergeable f, GMergeable g) => GMergeable (f :*: g) where - gsequenceAlt (a :*: b) = (:*:) <$> gsequenceAlt a <*> gsequenceAlt b + gmerge f (a :*: b) = (:*:) <$> gmerge f a <*> gmerge f b instance GMergeable [] where - gsequenceAlt (x:xs) = ((:) <$> x <|> pure identity) <*> gsequenceAlt xs - gsequenceAlt [] = pure [] + gmerge f (x:xs) = ((:) <$> f x <|> pure identity) <*> gmerge f xs + gmerge _ [] = pure []