OCaml |> operator

后端 未结 3 1956
旧时难觅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:13

    The |> operator represents reverse function application. It sounds complicated but it just means you can put the function (and maybe a few extra parameters) after the value you want to apply it to. This lets you build up something that looks like a Unix pipeline:

    # let ( |> ) x f = f x;;
    val ( |> ) : 'a -> ('a -> 'b) -> 'b = 
    # 0.0 |> sin |> exp;;
    - : float = 1.
    

    The notation Module.(expr) is used to open the module temporarily for the one expression. In other words, you can use names from the module directly in the expression, without having to prefix the module name.

提交回复
热议问题