File: stdcompat__option.ml.in

package info (click to toggle)
ocaml-stdcompat 14-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,152 kB
  • sloc: ml: 22,329; makefile: 211; sh: 120
file content (79 lines) | stat: -rw-r--r-- 1,337 bytes parent folder | download | duplicates (4)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
@BEGIN_FROM_4_08_0@
include Option
@END_FROM_4_08_0@
@BEGIN_BEFORE_4_08_0@
type 'a t = 'a option =
  | None
  | Some of 'a

let none = None

let some x = Some x

let value o ~default =
  match o with
  | None -> default
  | Some v -> v

let get o =
  match o with
  | None -> invalid_arg "option is None"
  | Some v -> v

let bind o f =
  match o with
  | None -> None
  | Some v -> f v

let join o =
  match o with
  | None -> None
  | Some v -> v

let map f o =
  match o with
  | None -> None
  | Some v -> Some (f v)

let fold ~none ~some o =
  match o with
  | None -> none
  | Some v -> some v

let iter f o =
  match o with
  | None -> ()
  | Some v -> f v

let is_none o = o = None

let is_some o = o <> None

let equal eq o o' =
  match o, o' with
  | None, None -> true
  | Some v, Some v' -> eq v v'
  | None, Some _ | Some _, None -> false

let compare cmp o o' =
  match o, o' with
  | None, None -> 0
  | None, Some _ -> -1
  | Some _, None -> 1
  | Some v, Some v' -> cmp v v'

let to_result ~none o =
  match o with
  | None -> Stdcompat__pervasives.Error none
  | Some v -> Stdcompat__pervasives.Ok v

let to_list o =
  match o with
  | None -> []
  | Some v -> [v]

let to_seq o =
  match o with
  | Some v -> fun () -> Stdcompat__seq.Cons (v, Stdcompat__seq.empty)
  | None -> Stdcompat__seq.empty
@END_BEFORE_4_08_0@