-
Notifications
You must be signed in to change notification settings - Fork 17
/
lens.jl
261 lines (193 loc) · 6.19 KB
/
lens.jl
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
export Lens, set, get, modify
export @lens
export set, get, modify
using ConstructionBase
export setproperties
export constructorof
import Base: get, hash, ==
using Base: getproperty
# used for hashing
function make_salt(s64::UInt64)::UInt
if UInt === UInt64
return s64
else
return UInt32(s64 >> 32) ^ UInt32(s64 & 0x00000000ffffffff)
end
end
"""
Lens
A `Lens` allows to access or replace deeply nested parts of complicated objects.
# Example
```jldoctest
julia> using Setfield
julia> struct T;a;b; end
julia> obj = T("AA", "BB")
T("AA", "BB")
julia> lens = @lens _.a
(@lens _.a)
julia> get(obj, lens)
"AA"
julia> set(obj, lens, 2)
T(2, "BB")
julia> obj
T("AA", "BB")
julia> modify(lowercase, obj, lens)
T("aa", "BB")
```
# Interface
Concrete subtypes of `Lens` have to implement
* `set(obj, lens, val)`
* `get(obj, lens)`
These must be pure functions, that satisfy the three lens laws:
```jldoctest; output = false, setup = :(using Setfield; (≅ = (==)); obj = (a="A", b="B"); lens = @lens _.a; val = 2; val1 = 10; val2 = 20)
@assert get(set(obj, lens, val), lens) ≅ val
# You get what you set.
@assert set(obj, lens, get(obj, lens)) ≅ obj
# Setting what was already there changes nothing.
@assert set(set(obj, lens, val1), lens, val2) ≅ set(obj, lens, val2)
# The last set wins.
# output
```
Here `≅` is an appropriate notion of equality or an approximation of it. In most contexts
this is simply `==`. But in some contexts it might be `===`, `≈`, `isequal` or something
else instead. For instance `==` does not work in `Float64` context, because
`get(set(obj, lens, NaN), lens) == NaN` can never hold. Instead `isequal` or
`≅(x::Float64, y::Float64) = isequal(x,y) | x ≈ y` are possible alternatives.
See also [`@lens`](@ref), [`set`](@ref), [`get`](@ref), [`modify`](@ref).
"""
abstract type Lens end
"""
modify(f, obj, l::Lens)
Replace a deeply nested part `x` of `obj` by `f(x)`. See also [`Lens`](@ref).
"""
function modify end
"""
get(obj, l::Lens)
Access a deeply nested part of `obj`. See also [`Lens`](@ref).
"""
function get end
"""
set(obj, l::Lens, val)
Replace a deeply nested part of `obj` by `val`. See also [`Lens`](@ref).
"""
function set end
@inline function modify(f, obj, l::Lens)
old_val = get(obj, l)
new_val = f(old_val)
set(obj, l, new_val)
end
struct IdentityLens <: Lens end
get(obj, ::IdentityLens) = obj
set(obj, ::IdentityLens, val) = val
struct PropertyLens{fieldname} <: Lens end
function get(obj, l::PropertyLens{field}) where {field}
getproperty(obj, field)
end
@inline function set(obj, l::PropertyLens{field}, val) where {field}
patch = (;field => val)
setproperties(obj, patch)
end
struct ComposedLens{LO, LI} <: Lens
outer::LO
inner::LI
end
function ==(l1::ComposedLens, l2::ComposedLens)
return l1.outer == l2.outer && l1.inner == l2.inner
end
const SALT_COMPOSEDLENS = make_salt(0xcf7322dcc2129a31)
hash(l::ComposedLens, h::UInt) = hash(l.outer, hash(l.inner, SALT_INDEXLENS + h))
"""
compose([lens₁, [lens₂, [lens₃, ...]]])
Compose `lens₁`, `lens₂` etc. There is one subtle point here:
While the two composition orders `(lens₁ ∘ lens₂) ∘ lens₃` and `lens₁ ∘ (lens₂ ∘ lens₃)` have equivalent semantics,
their performance may not be the same. The compiler tends to optimize right associative composition
(second case) better then left associative composition.
The compose function tries to use a composition order, that the compiler likes. The composition order is therefore not part of the stable API.
"""
function compose end
compose() = IdentityLens()
compose(l::Lens) = l
compose(::IdentityLens, ::IdentityLens) = IdentityLens()
compose(::IdentityLens, l::Lens) = l
compose(l::Lens, ::IdentityLens) = l
compose(outer::Lens, inner::Lens) = ComposedLens(outer, inner)
function compose(l1::Lens, ls::Lens...)
compose(l1, compose(ls...))
end
"""
lens₁ ∘ lens₂
Compose lenses `lens₁`, `lens₂`, ..., `lensₙ` to access nested objects.
# Example
```jldoctest
julia> using Setfield
julia> obj = (a = (b = (c = 1,),),);
julia> la = @lens _.a
lb = @lens _.b
lc = @lens _.c
lens = la ∘ lb ∘ lc
(@lens _.a.b.c)
julia> get(obj, lens)
1
```
"""
Base.:∘(l1::Lens, l2::Lens) = compose(l1, l2)
function get(obj, l::ComposedLens)
inner_obj = get(obj, l.outer)
get(inner_obj, l.inner)
end
function set(obj,l::ComposedLens, val)
inner_obj = get(obj, l.outer)
inner_val = set(inner_obj, l.inner, val)
set(obj, l.outer, inner_val)
end
struct IndexLens{I <: Tuple} <: Lens
indices::I
end
==(l1::IndexLens, l2::IndexLens) = l1.indices == l2.indices
const SALT_INDEXLENS = make_salt(0x8b4fd6f97c6aeed6)
hash(l::IndexLens, h::UInt) = hash(l.indices, SALT_INDEXLENS + h)
Base.@propagate_inbounds function get(obj, l::IndexLens)
getindex(obj, l.indices...)
end
Base.@propagate_inbounds function set(obj, l::IndexLens, val)
setindex(obj, val, l.indices...)
end
struct DynamicIndexLens{F} <: Lens
f::F
end
Base.@propagate_inbounds get(obj, I::DynamicIndexLens) = obj[I.f(obj)...]
Base.@propagate_inbounds set(obj, I::DynamicIndexLens, val) =
setindex(obj, val, I.f(obj)...)
"""
FunctionLens(f)
@lens f(_)
Lens with [`get`](@ref) method definition that simply calls `f`.
[`set`](@ref) method for each function `f` must be implemented manually.
Use `methods(set, (Any, Setfield.FunctionLens, Any))` to get a list of
supported functions.
Note that `FunctionLens` flips the order of composition; i.e.,
`(@lens f(_)) ∘ (@lens g(_)) == @lens g(f(_))`.
# Example
```jldoctest
julia> using Setfield
julia> obj = ((1, 2), (3, 4));
julia> lens = (@lens first(_)) ∘ (@lens last(_))
(@lens last(first(_)))
julia> get(obj, lens)
2
julia> set(obj, lens, '2')
((1, '2'), (3, 4))
```
# Implementation
To use `myfunction` as a lens, define a `set` method with the following
signature:
```julia
Setfield.set(obj, ::typeof(@lens myfunction(_)), val) = ...
```
`typeof` is used above instead of `FunctionLens` because how actual
type of `@lens myfunction(_)` is implemented is not the part of stable
API.
"""
struct FunctionLens{f} <: Lens end
FunctionLens(f) = FunctionLens{f}()
get(obj, ::FunctionLens{f}) where f = f(obj)