21

In F# I can't live without pipes (<| and |>)

let console(dashboard : Dashboard ref) = 
    let rec eat (command : string) =
        command.Split(' ','(',')') 
        |> Seq.filter(fun s -> s.Length <> 0)
        |> fun C ->
            (Seq.head C).ToUpper() |> fun head ->

Can I use <| and |> in OCaml?

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
cnd
  • 32,616
  • 62
  • 183
  • 313

1 Answers1

32

These are available since OCaml 4.01. However, <| is named @@ there, so it has the correct operator associativity.

Alternatively, you can either define them yourself:

let (|>) v f = f v
let (<|) f v = f v  (* or: *)
let (@@) f v = f v

Or you use Ocaml batteries included, which has the |> and <| operators defined in BatStd.

vog
  • 23,517
  • 11
  • 59
  • 75
LiKao
  • 10,408
  • 6
  • 53
  • 91
  • 2
    update: in 4.01.0 `|>` is included in the stdlib, and `@@` is defined the same as `|<`. – nlucaroni May 20 '14 at 13:04
  • @nlucaroni thanks! I wonder why they chose `@@` instead of something obviously complementary to forward piping? (`<|` seems like the best option to me, but `|<` at least uses analogous glyphs). – Shon Oct 30 '14 at 14:54
  • 2
    It's because of the associativity necessary in the grammar. `@` is used to start infix functions with right association, while `|` is used for left. This has been the standard for awhile. See: http://stackoverflow.com/questions/6150551/ocaml-why-i-cant-use-this-operator-infix – nlucaroni Oct 30 '14 at 16:36
  • The answer should be edited to reflect the current state of `|>` and `@@`. – vog Mar 11 '15 at 16:16
  • @vog I haven't followed the recent updates to ocaml for a while. Maybe you could add the updates? – LiKao Mar 12 '15 at 10:59