1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 06:41:45 +03:00

Extract a coalesceBy function taking a coalesce function.

This commit is contained in:
Rob Rix 2016-03-07 14:28:17 -05:00
parent 76ea86d0f4
commit 8595496032

View File

@ -55,8 +55,11 @@ instance Monoid a => Monoid (Both a) where
mappend a b = mappend <$> a <*> b
instance (PartialSemigroup a, Monoid a) => PartialSemigroup (Both a) where
coalesce a b = case coalesce <$> a <*> b of
Both (Just l, Just r) -> Just (both l r)
Both (Nothing, Just r) -> Just (both (fst a `mappend` fst b) r)
Both (Just l, Nothing) -> Just (both l (snd a `mappend` snd b))
Both (Nothing, Nothing) -> Nothing
coalesce = coalesceBy coalesce
coalesceBy :: Monoid a => (a -> a -> Maybe a) -> Both a -> Both a -> Maybe (Both a)
coalesceBy coalesce a b = case coalesce <$> a <*> b of
Both (Just l, Just r) -> Just (both l r)
Both (Nothing, Just r) -> Just (both (fst a `mappend` fst b) r)
Both (Just l, Nothing) -> Just (both l (snd a `mappend` snd b))
Both (Nothing, Nothing) -> Nothing