Commit 9cfabbb5 authored by Sven Tennie's avatar Sven Tennie Committed by Ben Gamari

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
parent 84f8e862
......@@ -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
......
......@@ -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*
......
......@@ -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, [''])
-- 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]
"<$> 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]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment