Anyone ever flip (<$>)

五迷三道 提交于 2019-11-27 11:44:16

问题


I found defining the following

(%)  = flip fmap

I can write code like this:

readFile "/etc/passwd" % lines % filter (not . null)

To me it makes more sense than the alternative:

filter (not . null) <$> lines <$> readFile "/etc/passwd"

Obviously, it's just a matter of order.

Does anyone else do this? Is there a valid reason not to write code like this?


回答1:


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

It can be imported with:

import Control.Lens.Operators ((<&>))



回答2:


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.




回答3:


-- (.) 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.




回答4:


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



来源:https://stackoverflow.com/questions/1678104/anyone-ever-flip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!