> printfn "%s")-6ren">
gpt4 book ai didi

f# - 在 F# 中 >> 运算符是什么意思?

转载 作者:行者123 更新时间:2023-12-03 08:33:22 25 4
gpt4 key购买 nike

我在此 sample 中的一些代码中注意到包含 >> 运算符:

let printTree =
tree >> Seq.iter (Seq.fold (+) "" >> printfn "%s")

>> 运算符是什么意思/做什么?

编辑:

非常感谢,现在清楚多了。
这是我为掌握它而生成的示例:
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

最佳答案

它是函数组合运算符。

更多信息 Chris Smith's blogpost .

Introducing the Function Composition operator (>>):

let inline (>>) f g x = 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

When executed prints ‘-4’.

关于f# - 在 F# 中 >> 运算符是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1904049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com