Flipped / reversed fmap (<$>)?

前端 未结 5 1374
忘掉有多难
忘掉有多难 2020-12-13 19:04

I found defining the following

(%)  = flip fmap

I can write code like this:

readFile \"/etc/passwd\" % lines % filter (not          


        
相关标签:
5条回答
  • 2020-12-13 19:23

    Your operator (%) is exactly the operator (<&>) from the lens package.

    It can be imported with:

    import Control.Lens.Operators ((<&>))
    
    0 讨论(0)
  • There is a similar function for the Applicative type class called <**>; it's a perfectly reasonable thing to want or use for Functor as well. Unfortunately, the semantics are a bit different for <**>, so it can't be directly widened to apply to Functor as well.

    0 讨论(0)
  • 2020-12-13 19:36
    (<&>) :: Functor f => f a -> (a -> b) -> f b 
    

    Now available from Data.Functor in base.

    https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Functor.html#v:-60--38--62-

    0 讨论(0)
  • 2020-12-13 19:37
    -- (.) is to (<$>) as flip (.) is to your (%).  
    

    I usually define (&) = flip (.) and it's just like your example, you can apply function composition backwords. Allows for easier to understand points-free code in my opinion.

    0 讨论(0)
  • 2020-12-13 19:45

    Personally I wouldn't use such an operators because then I have to learn two orders in which to read programs.

    0 讨论(0)
提交回复
热议问题