From 859549603202f1a8a0bfa545e95f7fac0267a6a4 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Mon, 7 Mar 2016 14:28:17 -0500 Subject: [PATCH] Extract a `coalesceBy` function taking a `coalesce` function. --- src/Data/Functor/Both.hs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Data/Functor/Both.hs b/src/Data/Functor/Both.hs index 7d136e24d..547340ac9 100644 --- a/src/Data/Functor/Both.hs +++ b/src/Data/Functor/Both.hs @@ -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