OCaml |> operator

后端 未结 3 1957
旧时难觅i
旧时难觅i 2021-01-01 15:47

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,         


        
3条回答
  •  情深已故
    2021-01-01 16:22

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

    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))
    

    that requires more parentheses.

提交回复
热议问题