livescript

What's the point of the back piping operator


LiveScript features both the forward and backward piping operator. The purpose of the forward piping is clear: [1, 2, 3] |> reverse |> tail |> sum translates to and is much clearer than sum(tail(reverse([1, 2, 3])));.

However, the purpose of the backward piping is a mystery to me: sum <| tail <| reverse <| [1, 2, 3] is exactly the same as just sum tail reverse [1, 2, 3], and as far as I can tell there's no difference in precedence.

So, what is the purpose of the <| operator in LiveScript?


Solution

  • It's useful as a section, when you want to make a function that applies its argument to a value:

    map (<| Math.PI), [(1 +), (2 -), (3 *), (4 /)]
    

    It's also consistent; there's |> so you'd kinda expect the reverse to exist as well.