summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Brooks <dmbrooks@live.co.uk>2019-11-05 20:12:18 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2019-11-06 08:07:15 -0500
commitb4fb232892ec420059e767bbf464bd09361aaefa (patch)
treee25e4c05aab7f92ef569a97bb22a2fa3c25c1337
parent97f9674b59e65932737b978a201aaf24a0894ad3 (diff)
downloadhaskell-b4fb232892ec420059e767bbf464bd09361aaefa.tar.gz
Adding examples to Semigroup/monoid
-rw-r--r--libraries/base/Data/Semigroup/Internal.hs6
-rw-r--r--libraries/base/GHC/Base.hs15
2 files changed, 21 insertions, 0 deletions
diff --git a/libraries/base/Data/Semigroup/Internal.hs b/libraries/base/Data/Semigroup/Internal.hs
index 2dfee142c9..4f75afccb5 100644
--- a/libraries/base/Data/Semigroup/Internal.hs
+++ b/libraries/base/Data/Semigroup/Internal.hs
@@ -285,6 +285,12 @@ instance Monad Product where
-- | Monoid under '<|>'.
--
+-- >>> getAlt (Alt (Just 12) <> Alt (Just 24))
+-- Just 12
+--
+-- >>> getAlt $ Alt Nothing <> Alt (Just 24)
+-- Just 24
+--
-- @since 4.8.0.0
newtype Alt f a = Alt {getAlt :: f a}
deriving ( Generic -- ^ @since 4.8.0.0
diff --git a/libraries/base/GHC/Base.hs b/libraries/base/GHC/Base.hs
index 5c60be83f0..fa702b9578 100644
--- a/libraries/base/GHC/Base.hs
+++ b/libraries/base/GHC/Base.hs
@@ -221,6 +221,9 @@ infixr 6 <>
-- @since 4.9.0.0
class Semigroup a where
-- | An associative operation.
+ --
+ -- >>> [1,2,3] <> [4,5,6]
+ -- [1,2,3,4,5,6]
(<>) :: a -> a -> a
-- | Reduce a non-empty list with '<>'
@@ -228,6 +231,9 @@ class Semigroup a where
-- The default definition should be sufficient, but this can be
-- overridden for efficiency.
--
+ -- >>> import Data.List.NonEmpty
+ -- >>> sconcat $ "Hello" :| [" ", "Haskell", "!"]
+ -- "Hello Haskell!"
sconcat :: NonEmpty a -> a
sconcat (a :| as) = go a as where
go b (c:cs) = b <> go c cs
@@ -243,6 +249,9 @@ class Semigroup a where
-- and monoids can upgrade this to execute in \(\mathcal{O}(1)\) by
-- picking @stimes = 'Data.Semigroup.stimesIdempotent'@ or @stimes =
-- 'stimesIdempotentMonoid'@ respectively.
+ --
+ -- >>> stimes 4 [1]
+ -- [1,1,1,1]
stimes :: Integral b => b -> a -> a
stimes = stimesDefault
@@ -266,6 +275,9 @@ class Semigroup a where
-- __NOTE__: 'Semigroup' is a superclass of 'Monoid' since /base-4.11.0.0/.
class Semigroup a => Monoid a where
-- | Identity of 'mappend'
+ --
+ -- >>> "Hello world" <> mempty
+ -- "Hello world"
mempty :: a
-- | An associative operation
@@ -284,6 +296,9 @@ class Semigroup a => Monoid a where
-- For most types, the default definition for 'mconcat' will be
-- used, but the function is included in the class definition so
-- that an optimized version can be provided for specific types.
+ --
+ -- >>> mconcat ["Hello", " ", "Haskell", "!"]
+ -- "Hello Haskell!"
mconcat :: [a] -> a
mconcat = foldr mappend mempty