简体   繁体   English

:: a - >(a - > b) - > b运算符(Haskell)

[英]:: a -> (a -> b) -> b operator (Haskell)

Writing Haskell programs I found myself in need of an operator like this. 编写Haskell程序我发现自己需要这样的运算符。

(|>) :: a -> (a -> b) -> b
(|>) = flip ($)
infixl 0 |>

I think it is useful when glueing many functions together. 我认为将多个功能粘合在一起很有用。

tText cs = someFun cs   |>
           lines        |>
           map (drop 4) |>
           reverse

I prefer it over . 我更喜欢它. because with |> the order in which the functions are applied is the same as the order in which the functions are written. 因为使用|>函数的应用顺序与函数的写入顺序相同。

tText' cs = reverse      . 
            map (drop 4) . 
            lines        . 
            someFun $ cs

Question is: is this ( |> ) something that already exists in the Prelude / some other basic library? 问题是:这是( |>Prelude /其他基本库中已存在的东西吗? Reimplementing simple stuff is something silly which I would like to avoid. 重新实现简单的东西是愚蠢的,我想避免。

A Hoogle search did not help. Hoogle搜索没有帮助。 Closest thing I found was >>> (Arrows), but it seems an overkill. 我找到的最近的东西是>>> (箭头),但它似乎有点矫枉过正。

No, there isn't anything in a standard library that I know of. 不,我所知道的标准库中没有任何东西。 Years and years ago a lot of my code imported my breif but handy Forwards module: 多年前我的很多代码都导入了我的breif但是方便的Forwards模块:

> module Forwards where

> infixl 0 |>
> infixl 9 .>

> (|>) = flip ($)
> (.>) = flip (.)

I even used the same name as you! 我甚至使用了和你一样的名字!

These days I don't use it much at all - I got used to the order that function composition uses. 这些天我根本不使用它 - 我习惯了函数组合使用的顺序。

Feel free to use your own handy shortcuts. 随意使用自己的方便快捷方式。

I also use $ less than I used to. 我也比以前少用$ Where I used to write 我曾经写过的地方

thing = this $ that arg $ an other $ it

Now I write 现在我写

thing = this . that arg . an other $ it

您还可以将(|>)定义为“翻转id”,并理解其工作原理,这是Haskell中使用的统一类型推断的一个很好的教训。

As far as my experience goes, I don't know any such library. 就我的经验而言,我不知道任何这样的图书馆。 And if such a library exists, I'd advise against using it. 如果存在这样的库,我建议不要使用它。


Unless you are using the lens package, I'd suggest not to use externally defined operators of that kind. 除非您使用镜头包,否则我建议不要使用此类外部定义的运算符。 (In case of that lens package, you really need and already have such an operator.) (如果是镜头包,你真的需要并且已经有这样的操作员。)

In my experience, IMHO and such ... 根据我的经验,恕我直言......等等......

In cases where the readability enhances with a forward composition in contrast to the usual composition (not only when dealing with lenses), it is beneficial to define a special operator in that module or locally via let or where . 如果与正常构图相比,可读性与前向构图相比增强(不仅在处理镜头时),在该模块中或通过letwhere本地定义特殊操作符是有益的。 For that, I tend to use single unicode symbols rather than ascii combos. 为此,我倾向于使用单个unicode符号而不是ascii组合。

(·) = flip (.)
infixl 1 (·)

(§) = ($) -- left associative, no flip
infixl 0 (§)

Some years ago(, when there were no lenses), I thought of defining my own module for these, too. 几年前(当没有镜头的时候),我也想过为这些镜头定义自己的模块。 But then I grow to use that module sooo infrequent that I tended to reinvent the wheel anyway. 但后来我逐渐使用那个模块,以至于我总是倾向于重新发明轮子。 To have these operators in a library may even increase the effort of reading the code: The reader has to look those rarely used operators up. 在库中使用这些操作符甚至可以增加读取代码的工作量:读者必须查看那些很少使用的操作符。 In that case, locally defined operators are way better. 在这种情况下,本地定义的运算符更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM