forked from pdonadeo/ocaml-lens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lens.mli
162 lines (112 loc) · 5.3 KB
/
lens.mli
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
(*
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.
*)
(** Functional lenses.
Based on F# implementation in {{:https://github.com/fsharp/fsharpx}FSharpx}
(see {{:https://github.com/fsharp/fsharpx/blob/master/src/FSharpx.Core/Lens.fs}src/FSharpx.Core/Lens.fs} for the original implementation)
@see <http://bugsquash.blogspot.com/2011/11/lenses-in-f.html> Lenses in F#
@see <http://stackoverflow.com/questions/8179485/updating-nested-immutable-data-structures> Stackoverflow question about Updating nested immutable data structures
@see <http://stackoverflow.com/questions/5767129/lenses-fclabels-data-accessor-which-library-for-structure-access-and-mutatio> Haskell libraries for structure access and mutation
@see <http://www.youtube.com/watch?v=efv0SQNde5Q> Functional lenses for Scala by Edward Kmett on YouTube
@see <http://patternsinfp.wordpress.com/2011/01/31/lenses-are-the-coalgebras-for-the-costate-comonad/> Lenses are the coalgebras for the costate comonad by Jeremy Gibbons
*)
(** Lens type definition *)
type ('a, 'b) t = {
get : 'a -> 'b;
(** Functional getter *)
set : 'b -> 'a -> 'a
(** Functional setter *)
}
(** Updates a value through a lens *)
val modify : ('a, 'b) t -> ('b -> 'b) -> 'a -> 'a
(** {3 Combinators} *)
(** Sequentially composes two lenses *)
val compose : ('a, 'b) t -> ('c, 'a) t -> ('c, 'b) t
(** Pairs two lenses *)
val pair : ('a, 'b) t -> ('c, 'd) t -> ('a * 'c, 'b * 'd) t
(** Pairs three lenses *)
val pair3 : ('a, 'b) t -> ('c, 'd) t -> ('e, 'f) t -> ('a * 'c * 'e, 'b * 'd * 'f) t
(** Selects a lens checking a predicate.
[cond pred lensTrue lensFalse]: [pred] is applied to source. If [true], [lensTrue] is selected. If [false], [lensFalse] is selected. *)
val cond : ('a -> bool) -> ('a, 'b) t -> ('a, 'b) t -> ('a, 'b) t
(** {3 State monad integration} *)
(** Gets a value from the state monad. *)
val get_state : ('a, 'b) t -> 'a -> 'b * 'a
(** Puts a value in the state monad. *)
val put_state : ('a, 'b) t -> 'b -> 'a -> unit * 'a
(** Modifies a value in the state monad. *)
val modify_state : ('a, 'b) t -> ('b -> 'b) -> 'a -> unit * 'a
(** {3 Stock lenses} *)
(** Trivial lens *)
val ignore : ('a, unit) t
(** Identity lens *)
val id : ('a, 'a) t
(** Gets/sets the first element in a pair *)
val first : ('a * 'b, 'a) t
(** Gets/sets the second element in a pair *)
val second : ('a * 'b, 'b) t
(** Gets/sets the first element in a list *)
val head : ('a list, 'a) t
(** Gets/sets the tail of a list *)
val tail : ('a list, 'a list) t
(** Lens for a particular key in a hashtable *)
val for_hash : 'a -> (('a, 'b) Hashtbl.t, 'b option) t
(** Lens for a particular key in an associative list *)
val for_assoc : 'a -> (('a * 'b) list, 'b option) t
(** Lens for a particular position in an array *)
val for_array : int -> ('a array, 'a) t
(** Lens for a particular position in a list *)
val for_list : int -> ('a list, 'a) t
(** Lens for extracting the value from an option type (same as Option.get) *)
val option_get : ('a option, 'a) t
(** {3 List combinators} *)
(** Creates a lens that maps the given lens in a list *)
val list_map : ('a, 'b) t -> ('a list, 'b list) t
(** {3 Isomorphism} *)
(** Applies an isomorphism to the value viewed through a lens *)
val xmap : ('a -> 'b) -> ('b -> 'a) -> ('c, 'a) t -> ('c, 'b) t
(** Infix operators *)
module Infix :
sig
val ( |. ) : 'a -> ('a, 'b) t -> 'b
(** Get operator *)
val ( ^= ) : ('a, 'b) t -> 'b -> 'a -> 'a
(** Set operator *)
val ( ^%= ) : ('a, 'b) t -> ('b -> 'b) -> 'a -> 'a
(** Mod operator *)
(** {3 Composition} *)
val ( |-- ) : ('a, 'b) t -> ('b, 'c) t -> ('a, 'c) t
(** Flipped compose operator *)
val ( --| ) : ('a, 'b) t -> ('c, 'a) t -> ('c, 'b) t
(** Compose operator *)
val ( *** ) : ('a, 'b) t -> ('c, 'd) t -> ('a * 'c, 'b * 'd) t
(** Pair operator *)
(** {3 Pseudo-imperatives} *)
val ( += ) : ('a, int) t -> int -> 'a -> 'a
val ( -= ) : ('a, int) t -> int -> 'a -> 'a
end
(** Infix operators for the state monad *)
module StateInfix :
sig
val ( ^=! ) : ('a, 'b) t -> 'b -> 'a -> unit * 'a
(** Set operator *)
(** {3 Pseudo-imperatives} *)
val ( +=! ) : ('a, int) t -> int -> 'a -> unit * 'a
val ( -=! ) : ('a, int) t -> int -> 'a -> unit * 'a
val ( @=! ) : ('a, 'b list) t -> 'b list -> 'a -> unit * 'a
end