Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use pipes in OCaml?

Tags:

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?

like image 694
cnd Avatar asked Jan 24 '12 11:01

cnd


People also ask

What does |> mean in OCaml?

It is a way to write your applications in the order of execution. As an exemple you could use it (I don't particularly think you should though) to avoid lets: 12 |> fun x -> e. instead of let x = 12 in e. For the Module.

What is the operator in OCaml?

Comparisons (Relational Operators) Most of these operators will look familiar: =, <>, <, <=, >, >=. OCaml distinguishes between structural equality and physical equality (essentially equality of the address of an object). = is structural equality and == is physical equality. Beware: <> is structural not-equals while !=


1 Answers

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.

like image 74
LiKao Avatar answered Sep 22 '22 07:09

LiKao