summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulie Moronuki <jdog74@gmail.com>2017-10-31 23:28:46 -0400
committerBen Gamari <ben@smart-cactus.org>2017-11-06 16:43:35 -0500
commit275ac8ef0a0081f16abbfb8934e10cf271573768 (patch)
tree1dbc921b1e8584beb8a5b118a9202564b30aef9f
parent66b5b3eef1aa9fa9f192a85847d34b2756bec33f (diff)
downloadhaskell-275ac8ef0a0081f16abbfb8934e10cf271573768.tar.gz
base: Add examples to Bifunctor documentation
-rw-r--r--libraries/base/Data/Bifunctor.hs37
1 files changed, 36 insertions, 1 deletions
diff --git a/libraries/base/Data/Bifunctor.hs b/libraries/base/Data/Bifunctor.hs
index 5441605ecf..04de5ad7f2 100644
--- a/libraries/base/Data/Bifunctor.hs
+++ b/libraries/base/Data/Bifunctor.hs
@@ -20,7 +20,15 @@ module Data.Bifunctor
import Control.Applicative ( Const(..) )
import GHC.Generics ( K1(..) )
--- | Formally, the class 'Bifunctor' represents a bifunctor
+-- | A bifunctor is a type constructor that takes
+-- two type arguments and is a functor in /both/ arguments. That
+-- is, unlike with 'Functor', a type constructor such as 'Either'
+-- does not need to be partially applied for a 'Bifunctor'
+-- instance, and the methods in this class permit mapping
+-- functions over the 'Left' value or the 'Right' value,
+-- or both at the same time.
+--
+-- Formally, the class 'Bifunctor' represents a bifunctor
-- from @Hask@ -> @Hask@.
--
-- Intuitively it is a bifunctor where both the first and second
@@ -59,22 +67,49 @@ class Bifunctor p where
-- | Map over both arguments at the same time.
--
-- @'bimap' f g ≡ 'first' f '.' 'second' g@
+ --
+ -- ==== __Examples__
+ -- >>> bimap toUpper (+1) ('j', 3)
+ -- ('J',4)
+ --
+ -- >>> bimap toUpper (+1) (Left 'j')
+ -- Left 'J'
+ --
+ -- >>> bimap toUpper (+1) (Right 3)
+ -- Right 4
bimap :: (a -> b) -> (c -> d) -> p a c -> p b d
bimap f g = first f . second g
+
-- | Map covariantly over the first argument.
--
-- @'first' f ≡ 'bimap' f 'id'@
+ --
+ -- ==== __Examples__
+ -- >>> first toUpper ('j', 3)
+ -- ('J',3)
+ --
+ -- >>> first toUpper (Left 'j')
+ -- Left 'J'
first :: (a -> b) -> p a c -> p b c
first f = bimap f id
+
-- | Map covariantly over the second argument.
--
-- @'second' ≡ 'bimap' 'id'@
+ --
+ -- ==== __Examples__
+ -- >>> second (+1) ('j', 3)
+ -- ('j',4)
+ --
+ -- >>> second (+1) (Right 3)
+ -- Right 4
second :: (b -> c) -> p a b -> p a c
second = bimap id
+
-- | @since 4.8.0.0
instance Bifunctor (,) where
bimap f g ~(a, b) = (f a, g b)