let multiply_by_two n =
n * 2;;
(* Here we have precedence issues, should 5 go to multiply_by_two or argument to print_int? *)
(* parens used to set precedence, setting 5 to multiply_by_two and the result of that to print_int *)
print_int (multiply_by_two 5);;
(* We can also use an infix operator |> to do this *)
multiply_by_two 5 |> print_int;;
(* |> takes what's on the left hand side and applies it as an argument to the right hand side expression *)
(* We can define |> ourself by doing function definition but with () *)
let (|>) x f = f x;;
(* Still works as expected *)
multiply_by_two 5 |> print_int;;
(* We can call it what ever, given that it is a valid operator identifier *)
let (-->) x f = f x;;
multiply_by_two 5 --> print_int;;
(* Another infix operator we could define is <| or $, which is in the other direction *)
let (<|) f x = f x;;
print_int <| multiply_by_two 5;;
(* We can also call infix operators in prefix style *)
(<|) print_int (multiply_by_two 5);;
(* This is also true for built-ins *)
(* Here we add space to not confuse with comment *)
print_int <| ( * ) 2 5;;
(* This means we also can partially apply operators! *)
(* So we can rewrite and simplify our mulitply_by_two operation *)
let multiply_by_two n =
n * 2;;
let multiply_by_two n = 2 * n;;
let multiply_by_two = ( * ) 2;;