5 ms·
I've never heard of Elixir, but I always assumed that the |> originated in F#. Ocaml has it pretty much standard, too, although you can define it in one line in
by mjt0229 12y ago
I've never heard of Elixir, but I always assumed that the |> originated in F#. Ocaml has it pretty much standard, too, although you can define it in one line in Ocaml:
let (|>) x f = f x
- reirob 12y agoAlso in Haskell [0]: (|>) :: Seq a -> a -> Seq a O(1). Add an element to the right end of a sequence. Mnemonic: a triangle with the single element at the pointy end. It seems to be in the containers package since 2005: [1] [0]: http://hackage.haskell.org/package/containers-0.5.5.1/docs/Data-Sequence.html#v:-124--62- http://hackage.haskell.org/package/containers-0.5.5.1/docs/D... [1]: https://github.com/haskell/containers/commit/1e61853dbd4b9fc6ecc58ad655990ea5e971fa13#diff-a4090fc3521182bd8d1ac467d170c6aaR40 https://github.com/haskell/containers/commit/1e61853dbd4b9fc...
- platz 12y agoWhich has a totally different meaning from the F# usage. '|>' from F# is the same as 'flip $' in haskell, or (&) imported from the lens library
- muraiki 12y agoIn Elixir, |> does not flip arguments. It lets you chain together multiple functions by inserting the result of the previous function as the first argument of the following function. Here's an example from http://www.theerlangelist.com/2014/01/why-elixir.html http://www.theerlangelist.com/2014/01/why-elixir.html The following code computes the sum of squares of all positive numbers of a list: list |> Enum.filter(&(&1 > 0)) |> Enum.map(&(&1 * &1)) |> Enum.reduce(0, &(&1 + &2))
- louthy 12y agoF# doesn't flip arguments either. It's an operator: let (|>) x f = f x Is basically saying: x |> f is equal to f x So in F# it's the same as Elixir, but the value x is applied to the function f (passed as the last argument). i.e. list |> List.filter (fun x -> x > 0) |> List.map (fun x -> x * x) |> List.reduce (fun s x -> s + x)
- muraiki 12y agoI was going off what platz said in another comment, that |> flips the arguments to |> in the same way Haskells flip function does, which I thought the type signature of the the F# |> also indicated. I'm sorry if I'm misunderstanding things. I think the difference though is that the |> in Elixir is actually a macro that modifies the following function call's first argument. So list |> Enum.filter(&(&1 > 0)) doesn't end up using filter as a curried function as one would find in Haskell: Enum.filter(&(&1 > 0)) list The end result is actually: Enum.filter(list, &(&1 > 0))