在F#中,>>是什么?运算符是什么意思? [英] In F# what does the >> operator mean?
问题描述
我在此示例中的一些代码中注意到了包含>>运算符:
I noticed in some code in this sample that contained the >> operator:
let printTree =
tree >> Seq.iter (Seq.fold (+) "" >> printfn "%s")
>>运算符是什么意思?
What does the >> operator mean/do?
非常感谢,现在更加清晰了. 这是我生成的示例,可以用来解决此问题:
Thanks very much, now it is much clearer. Here's my example I generated to get the hang of it:
open System
open System.IO
let read_lines path = File.ReadAllLines(path) |> Array.to_list
let trim line = (string line).Trim()
let to_upper line = (string line).ToUpper()
let new_list = [ for line in read_lines "myText.txt" -> line |> (trim >> to_upper) ]
printf "%A" new_list
推荐答案
它是函数组合运算符.
有关克里斯·史密斯博客文章的更多信息
介绍功能组成 运算符(>>):
Introducing the Function Composition operator (>>):
let inline (>>) f g x = g(f x)
读为:给定两个函数,f 和g,以及一个值x,计算出 x的f的结果并传递该结果 到g.有趣的是 您可以使用(>>)函数 并且只传递参数f和g, 结果是一个需要一个函数的函数 单个参数并产生 结果g(f(x)).
Which reads as: given two functions, f and g, and a value, x, compute the result of f of x and pass that result to g. The interesting thing here is that you can curry the (>>) function and only pass in parameters f and g, the result is a function which takes a single parameter and produces the result g ( f ( x ) ).
这是组成一个简单的例子 较小的功能:
Here's a quick example of composing a function out of smaller ones:
let negate x = x * -1
let square x = x * x
let print x = printfn "The number is: %d" x
let square_negate_then_print = square >> negate >> print
asserdo square_negate_then_print 2
执行时会打印"-4".
When executed prints ‘-4’.
这篇关于在F#中,>>是什么?运算符是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!