summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorSven Tennie <sven.tennie@gmail.com>2017-07-28 18:25:50 -0400
committerBen Gamari <ben@smart-cactus.org>2017-07-29 10:33:42 -0400
commit9cfabbb5267e72b8017d8dc04d8580f73f425aa8 (patch)
tree26e04fcfd989ebae6aa570f7d957a68b0b30828e /libraries
parent84f8e86248d47f619a94c68260876a1258e0a931 (diff)
downloadhaskell-9cfabbb5267e72b8017d8dc04d8580f73f425aa8.tar.gz
Add '<&>' operator to Data.Functor. '<&>' calls '<$>' with flipped arguments.
This was proposed by David Feuer (https://mail.haskell.org/pipermail/libraries/2016-August/027293.html) and solves #14029. The implementation is a copy of the '<&>' operator in Control.Lens.Lens. Add tests for following Data.Functor operators: '<$>', '<&>', '<$' and '$>'. '<&>' was added for solving #14029. '<$>', '<$' and '$>' were probably untested. Reviewers: austin, hvr, bgamari, RyanGlScott Reviewed By: RyanGlScott Subscribers: RyanGlScott, rwbarton, thomie Differential Revision: https://phabricator.haskell.org/D3800
Diffstat (limited to 'libraries')
-rw-r--r--libraries/base/Data/Functor.hs26
-rw-r--r--libraries/base/changelog.md2
-rw-r--r--libraries/base/tests/all.T1
-rw-r--r--libraries/base/tests/functorOperators.hs38
-rw-r--r--libraries/base/tests/functorOperators.stdout16
5 files changed, 83 insertions, 0 deletions
diff --git a/libraries/base/Data/Functor.hs b/libraries/base/Data/Functor.hs
index 62bb70927e..2c0fbc3f29 100644
--- a/libraries/base/Data/Functor.hs
+++ b/libraries/base/Data/Functor.hs
@@ -20,6 +20,7 @@ module Data.Functor
(<$),
($>),
(<$>),
+ (<&>),
void,
) where
@@ -74,6 +75,31 @@ infixl 4 <$>
infixl 4 $>
+-- | Flipped version of '<$>'.
+--
+-- @
+-- ('<&>') = 'flip' 'fmap'
+-- @
+--
+-- @since 4.11.0.0
+--
+-- ==== __Examples__
+-- Apply @(+1)@ to a list, a 'Data.Maybe.Just' and a 'Data.Either.Right':
+--
+-- >>> Just 2 <&> (+1)
+-- Just 3
+--
+-- >>> [1,2,3] <&> (+1)
+-- [2,3,4]
+--
+-- >>> Right 3 <&> (+1)
+-- Right 4
+--
+(<&>) :: Functor f => f a -> (a -> b) -> f b
+as <&> f = f <$> as
+
+infixl 1 <&>
+
-- | Flipped version of '<$'.
--
-- @since 4.7.0.0
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 0cfd9c1ba8..a9f2992513 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -10,6 +10,8 @@
* Add `infixl 9 !!` declaration for `Data.List.NonEmpty.!!`
+ * Add `<&>` operator to `Data.Functor` (#14029)
+
## 4.10.0.0 *April 2017*
* Bundled with GHC *TBA*
diff --git a/libraries/base/tests/all.T b/libraries/base/tests/all.T
index 4bd8084220..b52a5d922b 100644
--- a/libraries/base/tests/all.T
+++ b/libraries/base/tests/all.T
@@ -214,3 +214,4 @@ test('T13191',
['-O'])
test('T13525', when(opsys('mingw32'), skip), compile_and_run, [''])
test('T13097', normal, compile_and_run, [''])
+test('functorOperators', normal, compile_and_run, [''])
diff --git a/libraries/base/tests/functorOperators.hs b/libraries/base/tests/functorOperators.hs
new file mode 100644
index 0000000000..aea5dfda80
--- /dev/null
+++ b/libraries/base/tests/functorOperators.hs
@@ -0,0 +1,38 @@
+-- Test infix operators of 'Functor'
+
+import Data.Functor
+
+main :: IO ()
+main = do
+ testInfixFmap
+ testFlippedInfixFmap
+ testInfixReplace
+ testFlippedInfixReplace
+
+testInfixFmap :: IO ()
+testInfixFmap = do
+ print "<$> tests:"
+ print $ (+ 1) <$> Just 2 -- => Just 3
+ print (((+ 1) <$> Right 3) :: Either Int Int) -- => Right 4
+ print $ (+ 1) <$> [1, 2, 3] -- => [2,3,4]
+
+testFlippedInfixFmap :: IO ()
+testFlippedInfixFmap = do
+ print "<&> tests:"
+ print $ Just 2 <&> (+ 1) -- => Just 3
+ print ((Right 3 <&> (+ 1)) :: Either Int Int) -- => Right 4
+ print $ [1, 2, 3] <&> (+ 1) -- => [2,3,4]
+
+testInfixReplace :: IO ()
+testInfixReplace = do
+ print "<$ tests:"
+ print $ 42 <$ Just 1 -- => Just 42
+ print ((42 <$ Right 1) :: Either Int Int) -- => Right 42
+ print $ 42 <$ [1, 2, 3] -- => [42,42,42]
+
+testFlippedInfixReplace :: IO ()
+testFlippedInfixReplace = do
+ print "$> tests:"
+ print $ Just 1 $> 42 -- => Just 42
+ print ((Right 1 $> 42) :: Either Int Int) -- => Right 42
+ print $ [1, 2, 3] $> 42 -- => [42,42,42]
diff --git a/libraries/base/tests/functorOperators.stdout b/libraries/base/tests/functorOperators.stdout
new file mode 100644
index 0000000000..00a17ed3b8
--- /dev/null
+++ b/libraries/base/tests/functorOperators.stdout
@@ -0,0 +1,16 @@
+"<$> tests:"
+Just 3
+Right 4
+[2,3,4]
+"<&> tests:"
+Just 3
+Right 4
+[2,3,4]
+"<$ tests:"
+Just 42
+Right 42
+[42,42,42]
+"$> tests:"
+Just 42
+Right 42
+[42,42,42]