翻转/反转 fmap (<$>)?

发布于 2024-08-10 06:39:16 字数 335 浏览 2 评论 0 原文

我发现定义以下内容

(%)  = flip fmap

我可以编写这样的代码:

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

对我来说,它比替代方案更有意义:

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

显然,这只是一个顺序问题。

还有其他人这样做吗?有充分的理由不编写这样的代码吗?

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) <
gt; lines <
gt; 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

狼性发作 2024-08-17 06:39:16
(<&>) :: Functor f => f a -> (a -> b) -> f b 

现在可从 base 中的 Data.Functor 获取。

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

(<&>) :: 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-

浅忆 2024-08-17 06:39:16

您的运算符 (%) 正是运算符 (<&>) 来自镜头包装

它可以通过以下方式导入:

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

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

It can be imported with:

import Control.Lens.Operators ((<&>))
流心雨 2024-08-17 06:39:16

Applicative 类型类有一个类似的函数,名为 <**>;对于 Functor 来说,想要或使用它也是完全合理的。不幸的是,<**> 的语义有点不同,因此它不能直接扩展以适用于 Functor

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.

烟─花易冷 2024-08-17 06:39:16
-- (.) is to (<
gt;) as flip (.) is to your (%).  

我通常定义 (&) = Flip (.) ,就像你的例子一样,你可以应用函数组合后缀。在我看来,可以更容易地理解无点代码。

-- (.) is to (<
gt;) 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.

你怎么敢 2024-08-17 06:39:16

就我个人而言,我不会使用这样的运算符,因为那样我必须学习两种读取程序的顺序。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文