将 F# 管道运算符( <|、>>、<<)转换为 OCaml [英] Converting F# pipeline operators ( <|, >>, << ) to OCaml
问题描述
我正在将一些 F# 代码转换为 OCaml,并且我看到了这个管道运算符 <|
的很多用途,例如:
I'm converting some F# code to OCaml and I see a lot of uses of this pipeline operator <|
, for example:
let printPeg expr =
printfn "%s" <| pegToString expr
<|
运算符显然定义为:
# let ( <| ) a b = a b ;;
val ( <| ) : ('a -> 'b) -> 'a -> 'b = <fun>
我想知道为什么他们费心在 F# 中定义和使用这个运算符,是不是为了避免像这样输入括号?:
I'm wondering why they bother to define and use this operator in F#, is it just so they can avoid putting in parens like this?:
let printPeg expr =
Printf.printf "%s" ( pegToString expr )
据我所知,这就是将上面的 F# 代码转换为 OCaml,对吗?
As far as I can tell, that would be the conversion of the F# code above to OCaml, correct?
另外,我将如何在 Ocaml 中实现 F# 的 <<
和 >>
运算符?
Also, how would I implement F#'s <<
and >>
operators in Ocaml?
( |>
运算符似乎只是: let ( |> ) a b = b a ;;
)
( the |>
operator seems to simply be: let ( |> ) a b = b a ;;
)
推荐答案
直接来自 F# 源代码:
let inline (|>) x f = f x
let inline (||>) (x1,x2) f = f x1 x2
let inline (|||>) (x1,x2,x3) f = f x1 x2 x3
let inline (<|) f x = f x
let inline (<||) f (x1,x2) = f x1 x2
let inline (<|||) f (x1,x2,x3) = f x1 x2 x3
let inline (>>) f g x = g(f x)
let inline (<<) f g x = f(g x)
这篇关于将 F# 管道运算符( <|、>>、<<)转换为 OCaml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!