[commit: ghc] master: Add '<&>' operator to Data.Functor. '<&>' calls '<$>' with flipped arguments. (9cfabbb)

git at git.haskell.org git at git.haskell.org
Sat Jul 29 14:34:49 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/9cfabbb5267e72b8017d8dc04d8580f73f425aa8/ghc

>---------------------------------------------------------------

commit 9cfabbb5267e72b8017d8dc04d8580f73f425aa8
Author: Sven Tennie <sven.tennie at gmail.com>
Date:   Fri Jul 28 18:25:50 2017 -0400

    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


>---------------------------------------------------------------

9cfabbb5267e72b8017d8dc04d8580f73f425aa8
 libraries/base/Data/Functor.hs               | 26 +++++++++++++++++++
 libraries/base/changelog.md                  |  2 ++
 libraries/base/tests/all.T                   |  1 +
 libraries/base/tests/functorOperators.hs     | 38 ++++++++++++++++++++++++++++
 libraries/base/tests/functorOperators.stdout | 16 ++++++++++++
 5 files changed, 83 insertions(+)

diff --git a/libraries/base/Data/Functor.hs b/libraries/base/Data/Functor.hs
index 62bb709..2c0fbc3 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 0cfd9c1..a9f2992 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 4bd8084..b52a5d9 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 0000000..aea5dfd
--- /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 0000000..00a17ed
--- /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]



More information about the ghc-commits mailing list