Source file option.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
type 'a t = 'a option

let return (a: 'a): 'a t =
    Some a

let fail: 'a t =
    None


let (let* ) (m: 'a t) (f: 'a -> 'b t): 'b t =
    match m with
    | Some a ->
        f a
    | None ->
        None


let (>>=) = (let* )



let (<*>) (f: ('a -> 'b) t) (a: 'a t): 'b t =
    match f, a with
    | Some f, Some a ->
        Some (f a)
    | _, _ ->
        None



let map (f: 'a -> 'b): 'a t -> 'b t = function
    | None ->
        None
    | Some a ->
        Some (f a)



let to_list (m: 'a t): 'a list =
    match m with
    | Some a ->
        [a]
    | None ->
        []


let to_result (err: 'e): 'a t -> ('a, 'e) result = function
    | None ->
        Error err
    | Some a ->
        Ok a