OCaml |>操作员 [英] OCaml |> operator

查看:72
本文介绍了OCaml |>操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释|>运算符的作用吗?此代码摘自参考文献此处:

Could someone explain what the |> operator does? This code was taken from the reference here:

let m = PairsMap.(empty |> add (0,1) "hello" |> add (1,0) "world")

我可以看到它的作用,但是不知道如何应用|>运算符.

I can see what it does, but I wouldn't know how to apply the |> operator otherwise.

对此,我也不知道Module.()的语法在做什么.对此的解释也很好.

For that matter, I have no idea what the Module.() syntax is doing either. An explanation on that would be nice too.

推荐答案

Module.(e)等同于let open Module in e.在范围内介绍事物是一种简便的语法.

Module.(e) is equivalent to let open Module in e. It is a shorthand syntax to introduce things in scope.

运算符|>在模块Pervasives中定义为let (|>) x f = f x. (实际上,它被定义为外部基元,易于编译.在这里这并不重要.)反向应用程序函数使链接后续调用更加容易.没有它,您将需要写

The operator |> is defined in module Pervasives as let (|>) x f = f x. (In fact, it is defined as an external primitive, easier to compile. This is unimportant here.) It is the reverse application function, that makes it easier to chain successive calls. Without it, you would need to write

let m = PairsMap.(add (1,0) "world" (add (0,1) "hello" empty))

需要更多的括号.

这篇关于OCaml |>操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆