Skip to content

Add operator <%>, or similar, as <%> = flip fmap

Often, piping results through one function after another provides a good conceptual flow to the code. But when the functions are a mixture of monadic and pure, it can seem more convoluted than it actually is. For example, compare:

do
  a <- monadicResult
  let b = pureFunction2 . pureFunction 1 <$> a
  c <- monadicFunction1 b >>= monadicFunction2

to

c <- monadicFunction2 =<<
     monadicFunction1 =<<
     pureFunction2    <$>
     pureFunction1    <$>
     monadicResult

But this reading from bottom-to-up seems backwards, especially considering monadic code is more traditionally top-to-bottom, left-to-right.

I'd like to propose adding a new operator

infixl 1 <%>
(<%>) :: Functor f => f a -> (a -> b) -> f b
(<%>) = flip fmap

so that the following can be written. It is obvious what the intent of the code is. There are no unnecessary temporary variables. It conforms to typical monadic code direction. It is visually pleasing.

c <- monadicResult    <%>
     pureFunction1    <%>
     pureFunction2    >>=
     monadicFunction1 >>=
     monadicFunction2
Trac metadata
Trac field Value
Version 7.6.3
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Prelude
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information