let a => a + b;
List.map((a) => a + 1, [7, 23, 8, 4]);
let (a,b) => {
a + b
};
let rec factorial = x =>
switch x {
| x when x <= 2 => x
| x => x * factorial(x - 1)
};
let add = (a,b) => a + b;
let f = (b: ('a) => 'a) => b(1 + 2);
let f = (b: ('a) => 'a) => b();
due to partial applications there are no nullary functions. Any function without arguments is a shorhand for one argument () of type unit
Any function parenthesis can be pattern matched (destructured) with similar destructuring syntax
let cross = ((a1, a2, a3), (b1, b2, b3)) => (
a2 * b3 - a3 * b2,
a3 * b1 - a1 * b3,
a1 * b2 - a2 * b1,
);
let computation = (~x, ~y, ~z) => x + y / z;
let cross = (~vector1 as (a1, a2, a3), ~vector2 as (b1, b2, b3)) => (
a2 * b3 - a3 * b2,
a3 * b1 - a1 * b3,
a1 * b2 - a2 * b1,
);
let add = (~x=?, ~y=?, ()) =>
switch (x, y) {
| (Some(x'), Some(y')) => x' + y'
| (Some(x'), None) => x'
| (None, Some(y')) => y'
| (None, None) => 0
};
let add = (~x=0, ~y=0, ()) => x + y;
let add = (~x: int=0, ~y: int=0, ()) => x + y;
With optional parameters, you need at least one positional parameter
let someFn = (a: option('a)) => a
let result2 = None
add(~x=?someFn(Some(12)),~y=?result2)
let add = (a,b) => a + b
add(2)(3)
|>
let's you chain functions
[4, 2, 1, 3, 5]
|> List.map(x => x + 1)
|> List.filter(x => x < 5)
|> List.sort(compare);
let divTuple = (tuple) =>
switch tuple {
| (_, 0) => (-1)
| (x, y) => x / y
};
let divTuple =
fun
| (_, 0) => (-1)
| (x, y) => x / y;
operators are just functions
(+)(8, 1);
These charachters can be used:
- First charachter:
! $ % & * + - . / : < = > ? @ ^ | ~
followed by 0+ - First charachter:
#
followed by 1+
You can read more about it here: 2ality blog
Expressions: Ocaml manual