[GHC] #8587: Add operator <%>, or similar, as <%> = flip fmap

GHC ghc-devs at haskell.org
Sun Dec 1 17:36:22 UTC 2013


#8587: Add operator <%>, or similar, as <%> = flip fmap
-------------------------------------------+-------------------------------
       Reporter:  bunimo                   |             Owner:
           Type:  feature request          |            Status:  new
       Priority:  normal                   |         Milestone:  _|_
      Component:  Prelude                  |           Version:  7.6.3
       Keywords:                           |  Operating System:
   Architecture:  Unknown/Multiple         |  Unknown/Multiple
     Difficulty:  Easy (less than 1 hour)  |   Type of failure:
     Blocked By:                           |  None/Unknown
Related Tickets:                           |         Test Case:
                                           |          Blocking:
-------------------------------------------+-------------------------------
 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
 }}}

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8587>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list