package tablecloth-native

  1. Overview
  2. Docs

Source file TableclothOption.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
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
80
81
82
83
type 'a t = 'a option

let some a = Some a

let isSome = Option.is_some

let is_some = isSome

let isNone = Option.is_none

let is_none = isNone

let and_ ta tb = match isSome ta with true -> tb | false -> None

let or_ ta tb = match isSome ta with true -> ta | false -> tb

let orElse ta tb = match isSome tb with true -> tb | false -> ta

let or_else = orElse

let andThen t ~f = match t with Some x -> f x | None -> None

let and_then = andThen

let flatten = Option.join

let both a b = match (a, b) with Some a, Some b -> Some (a, b) | _ -> None

let map t ~f = Option.map f t

let map2 (ta : 'a t) (tb : 'b t) ~(f : 'a -> 'b -> 'c) : 'c t =
  match (ta, tb) with Some a, Some b -> Some (f a b) | _ -> None


let unwrap t ~default = match t with None -> default | Some value -> value

let unwrapUnsafe x =
  match x with
  | None ->
      raise (Invalid_argument "Option.unwrapUnsafe called with None")
  | Some x ->
      x


let unwrap_unsafe = unwrapUnsafe

let tap t ~f = Option.iter f t

let toArray t = match t with None -> [||] | Some value -> [| value |]

let to_array = toArray

let toList t = match t with None -> [] | Some value -> [ value ]

let to_list = toList

let equal equal a b =
  match (a, b) with
  | None, None ->
      true
  | Some a', Some b' ->
      equal a' b'
  | _ ->
      false


let compare compare a b =
  match (a, b) with
  | None, None ->
      0
  | Some a', Some b' ->
      compare a' b'
  | None, Some _ ->
      -1
  | Some _, None ->
      1


let ( |? ) t default = unwrap t ~default

let ( >>| ) t f = map t ~f

let ( >>= ) t f = andThen t ~f