Opened 5 months ago

Closed 5 months ago

#8587 closed feature request (invalid)

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

Reported by: bunimo Owned by:
Priority: normal Milestone:
Component: Prelude Version: 7.6.3
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Difficulty: Easy (less than 1 hour)
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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

Change History (1)

comment:1 Changed 5 months ago by monoidal

  • Resolution set to invalid
  • Status changed from new to closed

Proposals to add new functions or instances should be discussed on the libraries mailing list first. Once you get approval, you can paste a link to the discussion here.

Note: See TracTickets for help on using tickets.