Source file read_write_pair.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
84
85
86
87
88
89
90
91
92
93
94
open Core
module Key = struct
type t =
[ `Read
| `Write
]
[@@deriving sexp]
let flip = function
| `Read -> `Write
| `Write -> `Read
;;
end
type ('a, +'z) any =
{ mutable read : 'a
; mutable write : 'a
}
[@@deriving sexp]
module Immutable = struct
type 'a t = ('a, immutable) any [@@deriving sexp]
end
module Read_only = struct
type 'a t = ('a, read) any [@@deriving sexp]
end
module Mutable = struct
type 'a t = ('a, read_write) any [@@deriving sexp]
end
type 'a t = 'a Immutable.t [@@deriving sexp]
let create ~read ~write = { read; write }
let createi f = { read = f `Read; write = f `Write }
let create_both x = { read = x; write = x }
let create_fn f = { read = f (); write = f () }
let create_with key v ~zero =
match key with
| `Read -> { read = v; write = zero }
| `Write -> { read = zero; write = v }
;;
let copy { read; write } = { read; write }
let exists t ~f = f t.read || f t.write
let for_all t ~f = f t.read && f t.write
let iteri t ~f =
f `Read t.read;
f `Write t.write
;;
let iter t ~f =
f t.read;
f t.write
;;
let map t ~f = { read = f t.read; write = f t.write }
let mapi t ~f = { read = f `Read t.read; write = f `Write t.write }
let foldi t init ~f = f (f init (`Read, t.read)) (`Write, t.write)
let fold t init ~f = f (f init t.read) t.write
let get t key =
match key with
| `Read -> t.read
| `Write -> t.write
;;
let set t key value =
match key with
| `Read -> t.read <- value
| `Write -> t.write <- value
;;
let replace t key ~f =
match key with
| `Read -> t.read <- f t.read
| `Write -> t.write <- f t.write
;;
let replace_all t ~f =
t.read <- f `Read t.read;
t.write <- f `Write t.write
;;
module Export = struct
type ('a, 'z) read_write_ = ('a, 'z) any =
{ mutable read : 'a
; mutable write : 'a
}
end