package containers

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file CCEither.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
(* This file is free software, part of containers. See file "license" for more details. *)

type 'a iter = ('a -> unit) -> unit
type 'a equal = 'a -> 'a -> bool
type 'a ord = 'a -> 'a -> int
type 'a printer = Format.formatter -> 'a -> unit

(** {2 Basics} *)

type ('a, 'b) t = ('a, 'b) Either.t = Left of 'a | Right of 'b

let left l = Left l
let right r = Right r

let is_left = function
  | Left _ -> true
  | Right _ -> false

let is_right = function
  | Left _ -> false
  | Right _ -> true

let find_left = function
  | Left l -> Some l
  | Right _ -> None

let find_right = function
  | Left _ -> None
  | Right r -> Some r

let map_left f = function
  | Left l -> Left (f l)
  | Right r -> Right r

let map_right f = function
  | Left l -> Left l
  | Right r -> Right (f r)

let map ~left ~right = function
  | Left l -> Left (left l)
  | Right r -> Right (right r)

let fold ~left ~right = function
  | Left l -> left l
  | Right r -> right r

let iter = fold
let for_all = fold

let equal ~left ~right e1 e2 =
  match e1, e2 with
  | Left l1, Left l2 -> left l1 l2
  | Right r1, Right r2 -> right r1 r2
  | _ -> false

let compare ~left ~right e1 e2 =
  match e1, e2 with
  | Left _, Right _ -> -1
  | Right _, Left _ -> 1
  | Left l1, Left l2 -> left l1 l2
  | Right r1, Right r2 -> right r1 r2

(** {2 IO} *)

let pp ~left ~right fmt = function
  | Left l -> Format.fprintf fmt "Left@ (@[%a@])" left l
  | Right r -> Format.fprintf fmt "Right@ (@[%a@])" right r
OCaml

Innovation. Community. Security.