Why does the pipe operator work?

前端 未结 4 1103
一个人的身影
一个人的身影 2021-02-13 12:44

If the pipe operator is created like this:

let (|>) f g = g f

And used like this:

let result = [2;4;6] |> List.map (fun x         


        
4条回答
  •  滥情空心
    2021-02-13 13:36

    If you enter your definition of |> into fsi and look at the operator's signature derived by type inference you'll notice val ( |> ) : 'a -> ('a -> 'b) -> 'b, i.e. argument 'a being given to function ('a -> 'b) yields 'b.

    Now project this signature onto your expression [2;4;6] |> List.map (fun x -> x * x * x) and you'll get List.map (fun x -> x * x * x) [2;4;6], where the argument is list [2;4;6] and the function is partially applied function of one argument List.map (fun x -> x * x * x).

提交回复
热议问题