forked from pdonadeo/ocaml-lens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlens.ml
188 lines (143 loc) · 3.95 KB
/
lens.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
(*
Copyright (c) 2011-2012 Alessandro Strada
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*)
let (|-) f g x = g (f x)
type ('a, 'b) t = {
get : 'a -> 'b;
set : 'b -> 'a -> 'a
}
let modify l f a =
let value = l.get a in
let new_value = f value in
l.set new_value a
let _get a l = l.get a
let _set v a l = l.set v a
let _modify f l = modify l f
let compose l1 l2 = {
get = l2.get |- l1.get;
set = l1.set |- modify l2
}
let pair l1 l2 = {
get = (fun (a, b) -> (l1.get a, l2.get b));
set = (fun (a, c) (b, d) -> (l1.set a b, l2.set c d))
}
let pair3 l1 l2 l3 = {
get = (fun (a, b, c) -> (l1.get a, l2.get b, l3.get c));
set = (fun (a, c, e) (b, d, f) -> (l1.set a b, l2.set c d, l3.set e f))
}
let cond pred lt lf =
let choose a = if pred a then lt else lf in
{ get = (fun a -> choose a |> _get a);
set = (fun b a -> choose a |> _set b a)
}
let get_state l =
fun a -> _get a l, a
let put_state l v =
fun a -> (), _set v a l
let modify_state l f =
fun a -> (), _modify f l a
let ignore = {
get = ignore;
set = (fun _ a -> a)
}
let id = {
get = (fun a -> a);
set = (fun b a -> b)
}
let first = {
get = fst;
set = (fun v a -> v, snd a)
}
let second = {
get = snd;
set = (fun v a -> fst a, v)
}
let head = {
get = List.hd;
set = (fun v xs -> v :: List.tl xs)
}
let tail = {
get = List.tl;
set = (fun v xs -> List.hd xs :: v)
}
let for_hash key = {
get = (fun h ->
try
Some (Hashtbl.find h key)
with Not_found -> None);
set = (fun v h ->
match v with
Some value -> Hashtbl.add h key value; h
| None -> Hashtbl.remove h key; h)
}
let for_assoc key = {
get = (fun l ->
try
Some (List.assoc key l)
with Not_found -> None);
set = (fun v l ->
match v with
Some value ->
let l' = List.remove_assoc key l in
(key, value) :: l'
| None -> List.remove_assoc key l)
}
let for_array i = {
get = (fun a -> Array.get a i);
set = (fun v a -> let a' = Array.copy a in Array.set a' i v; a')
}
let for_list i = {
get = (fun xs -> List.nth xs i);
set = (fun v xs ->
List.fold_left
(fun (xs', j) x ->
(if i = j then v::xs' else x::xs'), (j+1))
([], 0)
xs |> fst |> List.rev)
}
let option_get = {
get = (function None -> raise Not_found | Some v -> v);
set = (fun v _ -> Some v)
}
let list_map l = {
get = List.map l.get;
set = List.map2 l.set
}
(* TODO: array_map *)
let xmap f g l = {
get = l.get |- f;
set = g |- l.set
}
module Infix =
struct
let (|.) = _get
let (^=) l v = fun a -> _set v a l
let (^%=) = modify
let (|--) l1 l2 = compose l2 l1
let (--|) = compose
let ( *** ) l1 l2 = pair l1 l2
let (+=) l v = _modify ((+) v) l
let (-=) l v = _modify ((-) v) l
end
module StateInfix =
struct
let (^=!) l v = put_state l v
let (+=!) l v = modify_state l ((+) v)
let (-=!) l v = modify_state l ((-) v)
let (@=!) l v = modify_state l (fun a -> a @ v)
end