Anyone ever flip (<$>)

2019-01-22 02:57发布

I found defining the following

(%)  = flip fmap

I can write code like the following:

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

to me it makes more sense then the alternative

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

Obviously its just a matter of order, does anyone else do this? is there a valid reason not to write code like this?

4条回答
Luminary・发光体
2楼-- · 2019-01-22 03:24

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

It can be imported with:

import Control.Lens.Operators ((<&>))
查看更多
一夜七次
3楼-- · 2019-01-22 03:30
-- (.) 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.

查看更多
forever°为你锁心
4楼-- · 2019-01-22 03:35

There is a similar function for the Applicative typeclass 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.

查看更多
三岁会撩人
5楼-- · 2019-01-22 03:37

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

查看更多
登录 后发表回答