是什么|>做? [英] What does |> do?

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

问题描述

来自 Real World OCaml ,第38页(请参见

From Real World OCaml, page 38(see https://realworldocaml.org/v1/en/html/variables-and-functions.html#prefix-and-infix-operators). It defines a function:

# let (|>) x f = f x ;;

并将其应用于字符串:

# let path = "/usr/bin:/usr/local/bin:/bin:/sbin";;
val path : string = "/usr/bin:/usr/local/bin:/bin:/sbin"
#   String.split ~on:':' path
  |> List.dedup ~compare:String.compare
  |> List.iter ~f:print_endline
  ;;
/bin
/sbin
/usr/bin
/usr/local/bin
- : unit = ()

我想知道-运营商实际上是做什么的?我很困惑,因为List.iter是一个函数,而print_endline也是一个函数.阅读代码后,我看不到String.compareList.dedup在给定的 path 字符串上的作用.我一遍遍地读这本书,上面写着:

I am wondering - what does the operator do, really? I am confused because List.iter is a function and print_endline is a function as well. Reading the code I do not see where String.compare or List.dedup operates on the given path string. I read the book over and over again, it says:

" 起初,这个操作符的目的不是很明显:它只需要一个值和一个函数并将该函数应用于该值.尽管进行了乏味的描述,但它仍然具有排序运算符的作用,其实质类似于在UNIX Shell中使用管道字符.例如,考虑以下代码,以打印出PATH的唯一元素.请注意,后面的List.dedup通过使用提供的比较功能对列表进行排序来从列表中删除重复项. "

" It's not quite obvious at first what the purpose of this operator is: it just takes a value and a function and applies the function to the value. Despite that bland-sounding description, it has the useful role of a sequencing operator, similar in spirit to using the pipe character in the UNIX shell. Consider, for example, the following code for printing out the unique elements of your PATH. Note that List.dedup that follows removes duplicates from a list by sorting the list using the provided comparison function. "

但是为什么这两个函数毕竟会在" path "上运行?有人可以启发我吗?

But why would the two functions operates on "path" after all? Can someone enlighten me?

推荐答案

好,您正在查看此运算符的完整定义.它接受一个值(在左侧)和一个函数(在右侧),并将函数应用于该值.使用以|开头的符号表示操作员向左关联.因此a |> b |> c等同于(a |> b) |> c.

Well, you're looking at the complete definition of this operator. It takes a value (at the left) and a function (at the right) and applies the function to the value. The use of a symbol starting with | means that the operator associates to the left. So a |> b |> c is equivalent to (a |> b) |> c.

似乎您对高阶函数比对|>运算符更困惑.是的,List.iter是一个函数,但是它将一个函数作为其第一个参数.因此print_endline也是一个功能也就不足为奇了.实际上,List.iter ~f: print_endline也是一个功能.此函数适用于表达式前一部分(列表)的值.这就是|>的作用.

It seems you're more confused by higher-order functions than by the |> operator. Yes, List.iter is a function, but it takes a function as its first argument. So it's no surprise that print_endline is a function also. In fact List.iter ~f: print_endline is also a function. This function is applied to the value of the previous part of the expression (a list). This is what |> does.

如果有帮助,则|>旨在与Unix命令行的|相似.您可以指定一个初始值和一系列要应用到它的函数,例如管道.

If it helps any, |> is intended to be similar to the | of the Unix command line. You can specify an initial value and a series of functions to be applied to it like a pipeline.

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

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