Add a Semigroup Element instance

This commit is contained in:
Ryan Scott 2018-01-25 12:11:48 -05:00
parent 6cc7c9a66f
commit 3e61caf889
2 changed files with 12 additions and 3 deletions

View file

@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeFamilies #-}
@ -39,7 +40,8 @@ import Data.ByteString.Lazy (ByteString)
import Data.Hashable (Hashable(..))
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as M
import Data.Monoid
import Data.Monoid (Monoid(..))
import Data.Semigroup (Semigroup(..))
import Data.String
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
@ -61,9 +63,14 @@ newtype Element = Element (HashMap Text Text -> Builder)
instance Show Element where
show e = LT.unpack . renderText $ e
instance Semigroup Element where
Element e1 <> Element e2 = Element (e1 <> e2)
instance Monoid Element where
mempty = Element mempty
mappend (Element e1) (Element e2) = Element (e1 <> e2)
#if !(MIN_VERSION_base(4,11,0))
mappend = (<>)
#endif
instance IsString Element where
fromString = toElement
@ -140,7 +147,7 @@ makeElementNoEnd name = Element $ \a -> go a
-- | Folding and monoidally appending attributes.
foldlMapWithKey :: Monoid m => (k -> v -> m) -> HashMap k v -> m
foldlMapWithKey f = M.foldlWithKey' (\m k v -> m <> f k v) mempty
foldlMapWithKey f = M.foldlWithKey' (\m k v -> m `mappend` f k v) mempty
s2b :: String -> Builder
s2b = BB.fromString

View file

@ -31,5 +31,7 @@ library
hashable >= 1.1 && < 1.3,
text >= 0.11 && < 1.3,
unordered-containers >= 0.2 && < 0.3
if !impl(ghc >= 8.0)
build-depends: semigroups >= 0.16.1 && < 0.19
hs-source-dirs: src
default-language: Haskell2010