diff options
author | Daniel Firth <locallycompact@gmail.com> | 2018-06-29 13:35:45 +0100 |
---|---|---|
committer | Daniel Firth <locallycompact@gmail.com> | 2018-07-04 09:50:15 +0100 |
commit | 95393cb17c2fe5f0903470a449e36728471759eb (patch) | |
tree | 9811fe894b6868c77fb30506d6fb55bb6552d913 | |
parent | 7f65f15e252214ebf2c560453cb45e506df14969 (diff) |
Add Semigroup instances for Monoids
-rw-r--r-- | Bustle/Marquee.hs | 4 | ||||
-rw-r--r-- | Bustle/Renderer.hs | 35 |
2 files changed, 24 insertions, 15 deletions
diff --git a/Bustle/Marquee.hs b/Bustle/Marquee.hs index 365a72d..77a9db5 100644 --- a/Bustle/Marquee.hs +++ b/Bustle/Marquee.hs @@ -50,9 +50,11 @@ newtype Marquee = Marquee { unMarquee :: String } toPangoMarkup :: Marquee -> String toPangoMarkup = unMarquee +instance Semigroup Marquee where + Marquee x <> Marquee y = Marquee (x <> y) + instance Monoid Marquee where mempty = Marquee "" - mappend x y = Marquee (unMarquee x `mappend` unMarquee y) mconcat = Marquee . mconcat . map unMarquee tag :: String -> Marquee -> Marquee diff --git a/Bustle/Renderer.hs b/Bustle/Renderer.hs index 42a282f..971a3c3 100644 --- a/Bustle/Renderer.hs +++ b/Bustle/Renderer.hs @@ -76,14 +76,16 @@ data Participants = deriving (Show, Eq) -instance Monoid Participants where - mempty = Participants Map.empty Map.empty - mappend (Participants sess1 sys1) (Participants sess2 sys2) = - Participants (f sess1 sess2) - (f sys1 sys2) +instance Semigroup Participants where + (<>) (Participants sess1 sys1) (Participants sess2 sys2) = + Participants (f sess1 sess2) + (f sys1 sys2) where f = Map.unionWith Set.union +instance Monoid Participants where + mempty = Participants Map.empty Map.empty + sessionParticipants :: Participants -> [(UniqueName, Set OtherName)] -- ^ sorted by column sessionParticipants = map (snd *** id) . Map.toAscList . pSession @@ -109,9 +111,9 @@ data RendererResult apps = -- -- This is extremely unpleasant but it's a Monday. There's a test case in -- Test/Renderer.hs because I don't trust myself. -instance Monoid apps => Monoid (RendererResult apps) where - mempty = RendererResult 0 0 [] [] mempty [] - mappend rr1 rr2 = RendererResult centreOffset topOffset shapes regions applications warnings + +instance Semigroup apps => Semigroup (RendererResult apps) where + rr1 <> rr2 = RendererResult centreOffset topOffset shapes regions applications warnings where centreOffset = rrCentreOffset rr1 `max` rrCentreOffset rr2 topOffset = rrTopOffset rr1 `max` rrTopOffset rr2 @@ -136,8 +138,12 @@ instance Monoid apps => Monoid (RendererResult apps) where regions = translatedRegions rr1 ++ translatedRegions rr2 - applications = rrApplications rr1 `mappend` rrApplications rr2 - warnings = rrWarnings rr1 `mappend` rrWarnings rr2 + applications = rrApplications rr1 <> rrApplications rr2 + warnings = rrWarnings rr1 <> rrWarnings rr2 + + +instance Monoid apps => Monoid (RendererResult apps) where + mempty = RendererResult 0 0 [] [] mempty [] processWithFilters :: (Log, Set UniqueName) -> (Log, Set UniqueName) @@ -229,12 +235,13 @@ data RendererOutput = deriving (Show) -instance Monoid RendererOutput where - mempty = RendererOutput [] [] [] - mappend (RendererOutput s1 r1 w1) - (RendererOutput s2 r2 w2) = RendererOutput (s1 ++ s2) +instance Semigroup RendererOutput where + (<>) (RendererOutput s1 r1 w1) + (RendererOutput s2 r2 w2) = RendererOutput (s1 ++ s2) (r1 ++ r2) (w1 ++ w2) +instance Monoid RendererOutput where + mempty = RendererOutput [] [] [] data BusState = BusState { apps :: Applications |