What's the difference between -> and |> in reasonml?

后端 未结 2 1977
暖寄归人
暖寄归人 2021-02-02 14:03

A period of intense googling provided me with some examples where people use both types of operators in one code, but generally they look just like two ways of doing one thing,

2条回答
  •  无人及你
    2021-02-02 14:28

    |> is usually called 'pipe-forward'. It's a helper function that's used in the wider OCaml community, not just ReasonML. It 'injects' the argument on the left as the last argument into the function on the right:

    0 |> f       == f(0)
    0 |> g(1)    == g(1, 0)
    0 |> h(1, 2) == h(1, 2, 0)
    // and so on
    

    -> is called 'pipe-first', and it's a new syntax sugar that injects the argument on the left into the first argument position of the function or data constructor on the right:

    0 -> f       == f(0)
    0 -> g(1)    == g(0, 1)
    0 -> h(1, 2) == h(0, 1, 2)
    0 -> Some    == Some(0)
    

    Note that -> is specific to BuckleScript i.e. when compiling to JavaScript. It's not available when compiling to native and is thus not portable. More details here: https://reasonml.github.io/docs/en/pipe-first

提交回复
热议问题