# semigroup / Data / Semigroup.hs

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134``` ```-- | A semigroup is a binary associative operation. module Data.Semigroup ( Semigroup((.++.)) , (<++>) , Max(..) , Min(..) , BoundedMax(..) , BoundedMin(..) ) where import Data.Monoid import Data.Sequence import qualified Data.Set as S import qualified Data.Map as M import qualified Data.IntSet as IS import qualified Data.IntMap as IM import qualified Data.ByteString as B import Control.Monad.Identity import Control.Applicative -- | A binary operation that must satisfy associativity. Unlike a @Monoid@, an identity in not essential. class Semigroup a where (.++.) :: a -> a -> a -- | A binary associative operation lifted into an applicative functor. (<++>) :: (Applicative f, Semigroup a) => f a -> f a -> f a (<++>) = liftA2 (.++.) instance Monoid a => Semigroup (Identity a) where (.++.) = liftM2 mappend instance Semigroup () where _ .++. _ = () instance Semigroup b => Semigroup (a -> b) where (.++.) = (<++>) instance Semigroup a => Semigroup (IO a) where (.++.) = (<++>) instance (Semigroup a, Semigroup b) => Semigroup (a, b) where (a1, b1) .++. (a2, b2) = (a1 .++. a2, b1 .++. b2) instance (Semigroup a, Semigroup b, Semigroup c) => Semigroup (a, b, c) where (a1, b1, c1) .++. (a2, b2, c2) = (a1 .++. a2, b1 .++. b2, c1 .++. c2) instance (Semigroup a, Semigroup b, Semigroup c, Semigroup d) => Semigroup (a, b, c, d) where (a1, b1, c1, d1) .++. (a2, b2, c2, d2) = (a1 .++. a2, b1 .++. b2, c1 .++. c2, d1 .++. d2) instance (Semigroup a, Semigroup b, Semigroup c, Semigroup d, Semigroup e) => Semigroup (a, b, c, d, e) where (a1, b1, c1, d1, e1) .++. (a2, b2, c2, d2, e2) = (a1 .++. a2, b1 .++. b2, c1 .++. c2, d1 .++. d2, e1 .++. e2) instance Semigroup Ordering where a .++. b = runIdentity (Identity a .++. Identity b) instance Semigroup All where a .++. b = runIdentity (Identity a .++. Identity b) instance Semigroup Any where a .++. b = runIdentity (Identity a .++. Identity b) instance Semigroup a => Semigroup (Dual a) where Dual a .++. Dual b = Dual (b .++. a) instance Semigroup (Endo a) where a .++. b = runIdentity (Identity a .++. Identity b) instance Num a => Semigroup (Product a) where a .++. b = runIdentity (Identity a .++. Identity b) instance Num a => Semigroup (Sum a) where a .++. b = runIdentity (Identity a .++. Identity b) instance Semigroup a => Semigroup (Maybe a) where Nothing .++. b = b a .++. Nothing = a Just a .++. Just b = Just (a .++. b) instance Semigroup (First a) where a .++. b = runIdentity (Identity a .++. Identity b) instance Semigroup (Last a) where a .++. b = runIdentity (Identity a .++. Identity b) instance Semigroup [a] where (.++.) = (++) instance Semigroup (Seq a) where (.++.) = mappend instance Ord a => Semigroup (S.Set a) where (.++.) = mappend instance Ord k => Semigroup (M.Map k v) where (.++.) = mappend instance Semigroup IS.IntSet where (.++.) = mappend instance Semigroup (IM.IntMap v) where (.++.) = mappend instance Semigroup B.ByteString where (.++.) = mappend newtype Max a = Max { runMax :: a } deriving (Eq, Ord, Show, Read) instance Ord a => Semigroup (Max a) where Max a .++. Max b = Max (a `max` b) newtype Min a = Min { runMin :: a } deriving (Eq, Ord, Show, Read) instance Ord a => Semigroup (Min a) where Min a .++. Min b = Min (a `min` b) newtype BoundedMax a = BoundedMax { runBoundedMax :: Maybe a } deriving (Eq, Ord, Show, Read) instance Ord a => Monoid (BoundedMax a) where mempty = BoundedMax Nothing mappend = max newtype BoundedMin a = BoundedMin { runBoundedMin :: Maybe a } deriving (Eq, Ord, Show, Read) instance Ord a => Monoid (BoundedMin a) where mempty = BoundedMin Nothing mappend = min ```
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.