9
9
"""
10
10
pred_lens(p, n)
11
11
12
- Return a `Vector` of `Setfield.Lens`es for accessing all nodes/fields in `n` conforming to predicate `p`.
12
+ Return a `Vector` of `Accessors.jl` lenses for accessing all nodes/fields in `n` conforming to
13
+ predicate `p`.
13
14
14
15
# Examples
15
16
```jldoctest
@@ -20,18 +21,35 @@ ProductNode # 2 obs, 16 bytes
20
21
╰── ArrayNode(2×2 Array with Int64 elements) # 2 obs, 80 bytes
21
22
22
23
julia> pred_lens(x -> x isa ArrayNode, n)
23
- 1-element Vector{Setfield.ComposedLens{Setfield.PropertyLens{:data}, Setfield.IndexLens{Tuple{Int64}}} }:
24
- (@lens _.data[2])
24
+ 1-element Vector{Any }:
25
+ (@optic _.data[2])
25
26
```
26
27
27
28
See also: [`list_lens`](@ref), [`find_lens`](@ref), [`findnonempty_lens`](@ref).
28
29
"""
29
- pred_lens (p:: Function , n) = _pred_lens (p, n)
30
+ function pred_lens (p:: Function , n)
31
+ result = Any[]
32
+ _pred_lens! (p, n, (), result)
33
+ return result
34
+ end
35
+
36
+ _pred_lens! (p:: Function , x, l, result) = p (x) && push! (result, Accessors. opticcompose (l... ))
37
+ function _pred_lens! (p:: Function , n:: T , l, result) where T <: AbstractMillStruct
38
+ p (n) && push! (result, Accessors. opticcompose (l... ))
39
+ for k in fieldnames (T)
40
+ _pred_lens! (p, getproperty (n, k), (l... , PropertyLens {k} ()), result)
41
+ end
42
+ end
43
+ function _pred_lens! (p:: Function , n:: Union{Tuple, NamedTuple} , l, result)
44
+ for i in eachindex (n)
45
+ _pred_lens! (p, n[i], (l... , IndexLens ((i,))), result)
46
+ end
47
+ end
30
48
31
49
"""
32
50
list_lens(n)
33
51
34
- Return a `Vector` of `Setfield.Lens`es for accessing all nodes/fields in `n`.
52
+ Return a `Vector` of `Accessors.jl` lenses for accessing all nodes/fields in `n`.
35
53
36
54
# Examples
37
55
```jldoctest
@@ -42,16 +60,16 @@ ProductNode # 2 obs, 16 bytes
42
60
╰── ArrayNode(2×2 Array with Int64 elements) # 2 obs, 80 bytes
43
61
44
62
julia> list_lens(n)
45
- 9-element Vector{Lens }:
46
- (@lens _ )
47
- (@lens _.data[1])
48
- (@lens _.data[1].data)
49
- (@lens _.data[1].bags)
50
- (@lens _.data[1].metadata)
51
- (@lens _.data[2])
52
- (@lens _.data[2].data)
53
- (@lens _.data[2].metadata)
54
- (@lens _.metadata)
63
+ 9-element Vector{Any }:
64
+ identity (generic function with 1 method )
65
+ (@optic _.data[1])
66
+ (@optic _.data[1].data)
67
+ (@optic _.data[1].bags)
68
+ (@optic _.data[1].metadata)
69
+ (@optic _.data[2])
70
+ (@optic _.data[2].data)
71
+ (@optic _.data[2].metadata)
72
+ (@optic _.metadata)
55
73
```
56
74
57
75
See also: [`pred_lens`](@ref), [`find_lens`](@ref), [`findnonempty_lens`](@ref).
@@ -61,7 +79,8 @@ list_lens(n) = pred_lens(t -> true, n)
61
79
"""
62
80
findnonempty_lens(n)
63
81
64
- Return a `Vector` of `Setfield.Lens`es for accessing all nodes/fields in `n` that have at least one observation.
82
+ Return a `Vector` of `Accessors.jl` lenses for accessing all nodes/fields in `n` that contain at
83
+ least one observation.
65
84
66
85
# Examples
67
86
```jldoctest
@@ -72,10 +91,10 @@ ProductNode # 2 obs, 16 bytes
72
91
╰── ArrayNode(2×2 Array with Int64 elements) # 2 obs, 80 bytes
73
92
74
93
julia> findnonempty_lens(n)
75
- 3-element Vector{Lens }:
76
- (@lens _ )
77
- (@lens _.data[1])
78
- (@lens _.data[2])
94
+ 3-element Vector{Any }:
95
+ identity (generic function with 1 method )
96
+ (@optic _.data[1])
97
+ (@optic _.data[2])
79
98
```
80
99
81
100
See also: [`pred_lens`](@ref), [`list_lens`](@ref), [`find_lens`](@ref).
@@ -85,8 +104,8 @@ findnonempty_lens(n) = pred_lens(t -> t isa AbstractMillNode && numobs(t) > 0, n
85
104
"""
86
105
find_lens(n, x)
87
106
88
- Return a `Vector` of `Setfield.Lens`es for accessing all nodes/fields in `n` that return `true` when
89
- compared to `x` using `Base.===`.
107
+ Return a `Vector` of `Accessors.jl` lenses for accessing all nodes/fields in `n` that return `true`
108
+ when compared to `x` using `Base.===`.
90
109
91
110
# Examples
92
111
```jldoctest
@@ -97,30 +116,19 @@ ProductNode # 2 obs, 16 bytes
97
116
╰── ArrayNode(2×2 Array with Int64 elements) # 2 obs, 80 bytes
98
117
99
118
julia> find_lens(n, n.data[1])
100
- 1-element Vector{Setfield.ComposedLens{Setfield.PropertyLens{:data}, Setfield.IndexLens{Tuple{Int64}}} }:
101
- (@lens _.data[1])
119
+ 1-element Vector{Any }:
120
+ (@optic _.data[1])
102
121
```
103
122
104
123
See also: [`pred_lens`](@ref), [`list_lens`](@ref), [`findnonempty_lens`](@ref).
105
124
"""
106
125
find_lens (n, x) = pred_lens (t -> t ≡ x, n)
107
126
108
- _pred_lens (p:: Function , n) = p (n) ? [IdentityLens ()] : Lens[]
109
- function _pred_lens (p:: Function , n:: T ) where T <: AbstractMillStruct
110
- res = [map (l -> PropertyLens {k} () ∘ l, _pred_lens (p, getproperty (n, k))) for k in fieldnames (T)]
111
- res = vcat (filter (! isempty, res)... )
112
- p (n) ? [IdentityLens (); res] : res
113
- end
114
- function _pred_lens (p:: Function , n:: Union{Tuple, NamedTuple} )
115
- res = [map (l -> IndexLens (tuple (i)) ∘ l, _pred_lens (p, n[i])) for i in eachindex (n)]
116
- vcat (filter (! isempty, res)... )
117
- end
118
-
119
127
"""
120
128
code2lens(n, c)
121
129
122
- Convert code `c` from [HierarchicalUtils.jl](@ref) traversal to a `Vector` of `Setfield.Lens` such
123
- that they access each node in tree egal to `n` .
130
+ Convert code `c` from [HierarchicalUtils.jl](@ref) traversal to a `Vector` of `Accessors.jl`
131
+ lenses such that they access each node in tree `n` egal to node under code `c` in the tree .
124
132
125
133
# Examples
126
134
```jldoctest
@@ -133,8 +141,8 @@ ProductNode [""] # 2 obs, 16 bytes
133
141
╰── ArrayNode(2×2 Array with Int64 elements) ["U"] # 2 obs, 80 bytes
134
142
135
143
julia> code2lens(n, "U")
136
- 1-element Vector{Setfield.ComposedLens{Setfield.PropertyLens{:data}, Setfield.IndexLens{Tuple{Int64}}} }:
137
- (@lens _.data[2])
144
+ 1-element Vector{Any }:
145
+ (@optic _.data[2])
138
146
```
139
147
140
148
See also: [`lens2code`](@ref).
@@ -144,8 +152,8 @@ code2lens(n::AbstractMillStruct, c::AbstractString) = find_lens(n, n[c])
144
152
"""
145
153
lens2code(n, l)
146
154
147
- Convert `Setfield.Lens` l to a `Vector` of codes from [HierarchicalUtils.jl](@ref) traversal such
148
- that they access each node in tree egal to `n `.
155
+ Convert `Accessors.jl` lens `l` to a `Vector` of codes from [HierarchicalUtils.jl](@ref) traversal
156
+ such that they access each node in tree `n` egal to node accessible by lens `l `.
149
157
150
158
# Examples
151
159
```jldoctest
@@ -157,20 +165,27 @@ ProductNode [""] # 2 obs, 16 bytes
157
165
│ ╰── ∅ ["M"]
158
166
╰── ArrayNode(2×2 Array with Int64 elements) ["U"] # 2 obs, 80 bytes
159
167
160
- julia> lens2code(n, (@lens _.data[2]))
168
+ julia> lens2code(n, (@optic _.data[2]))
161
169
1-element Vector{String}:
162
170
"U"
163
171
172
+ julia> lens2code(n, (@optic _.data[∗]))
173
+ 2-element Vector{String}:
174
+ "E"
175
+ "U"
176
+
164
177
```
165
178
166
179
See also: [`code2lens`](@ref).
167
180
"""
168
- lens2code (n:: AbstractMillStruct , l:: Lens ) = HierarchicalUtils. find_traversal (n, get (n, l))
181
+ lens2code (n:: AbstractMillStruct , l) = mapreduce (vcat, Accessors. getall (n, l)) do x
182
+ HierarchicalUtils. find_traversal (n, x)
183
+ end
169
184
170
185
"""
171
186
model_lens(m, l)
172
187
173
- Convert `Setfield.Lens` `l` for a data node to a new lens for accessing the same location in model `m`.
188
+ Convert `Accessors.jl` lens `l` for a data node to a new lens for accessing the same location in model `m`.
174
189
175
190
# Examples
176
191
```jldoctest
@@ -187,26 +202,26 @@ ProductModel ↦ Dense(20 => 10) # 2 arrays, 210 params, 920 bytes
187
202
│ ╰── ArrayModel(Dense(2 => 10)) # 2 arrays, 30 params, 200 bytes
188
203
╰── ArrayModel(Dense(2 => 10)) # 2 arrays, 30 params, 200 bytes
189
204
190
- julia> model_lens(m, (@lens _.data[2]))
191
- (@lens _.ms[2])
205
+ julia> model_lens(m, (@optic _.data[2]))
206
+ (@optic _.ms[2])
192
207
```
193
208
194
209
See also: [`data_lens`](@ref).
195
210
"""
196
- function model_lens (model, lens:: ComposedLens )
197
- outerlens = model_lens (model, lens. outer )
198
- outerlens ∘ model_lens (get ( model, outerlens) , lens. inner )
211
+ function model_lens (model, lens:: ComposedOptic )
212
+ innerlens = model_lens (model, lens. inner )
213
+ innerlens ⨟ model_lens (only ( getall ( model, innerlens)) , lens. outer )
199
214
end
200
- model_lens (:: ArrayModel , :: PropertyLens{:data} ) = @lens _. m
201
- model_lens (:: BagModel , :: PropertyLens{:data} ) = @lens _. im
202
- model_lens (:: ProductModel , :: PropertyLens{:data} ) = @lens _. ms
215
+ model_lens (:: ArrayModel , :: PropertyLens{:data} ) = @optic _. m
216
+ model_lens (:: BagModel , :: PropertyLens{:data} ) = @optic _. im
217
+ model_lens (:: ProductModel , :: PropertyLens{:data} ) = @optic _. ms
203
218
model_lens (:: Union{NamedTuple, Tuple} , lens:: IndexLens ) = lens
204
- model_lens (:: Union{AbstractMillModel, NamedTuple, Tuple} , lens:: IdentityLens ) = lens
219
+ model_lens (:: Union{AbstractMillModel, NamedTuple, Tuple} , lens:: typeof (identity) ) = lens
205
220
206
221
"""
207
222
data_lens(n, l)
208
223
209
- Convert `Setfield.Lens` `l` for a model node to a new lens for accessing the same location in data node `n`.
224
+ Convert `Accessors.jl` lens `l` for a model node to a new lens for accessing the same location in data node `n`.
210
225
211
226
# Examples
212
227
```jldoctest
@@ -222,21 +237,21 @@ ProductModel ↦ Dense(20 => 10) # 2 arrays, 210 params, 920 bytes
222
237
│ ╰── ArrayModel(Dense(2 => 10)) # 2 arrays, 30 params, 200 bytes
223
238
╰── ArrayModel(Dense(2 => 10)) # 2 arrays, 30 params, 200 bytes
224
239
225
- julia> data_lens(n, (@lens _.ms[2]))
226
- (@lens _.data[2])
240
+ julia> data_lens(n, (@optic _.ms[2]))
241
+ (@optic _.data[2])
227
242
```
228
243
229
244
See also: [`data_lens`](@ref).
230
245
"""
231
- function data_lens (ds, lens:: ComposedLens )
232
- outerlens = data_lens (ds, lens. outer )
233
- outerlens ∘ data_lens (get ( ds, outerlens) , lens. inner )
246
+ function data_lens (ds, lens:: ComposedOptic )
247
+ innerlens = data_lens (ds, lens. inner )
248
+ innerlens ⨟ data_lens (only ( getall ( ds, innerlens)) , lens. outer )
234
249
end
235
- data_lens (:: ArrayNode , :: PropertyLens{:m} ) = @lens _. data
236
- data_lens (:: AbstractBagNode , :: PropertyLens{:im} ) = @lens _. data
237
- data_lens (:: AbstractProductNode , :: PropertyLens{:ms} ) = @lens _. data
250
+ data_lens (:: ArrayNode , :: PropertyLens{:m} ) = @optic _. data
251
+ data_lens (:: AbstractBagNode , :: PropertyLens{:im} ) = @optic _. data
252
+ data_lens (:: AbstractProductNode , :: PropertyLens{:ms} ) = @optic _. data
238
253
data_lens (:: Union{NamedTuple, Tuple} , lens:: IndexLens ) = lens
239
- data_lens (:: Union{AbstractMillNode, NamedTuple, Tuple} , lens:: IdentityLens ) = lens
254
+ data_lens (:: Union{AbstractMillNode, NamedTuple, Tuple} , lens:: typeof (identity) ) = lens
240
255
241
256
"""
242
257
replacein(n, old, new)
0 commit comments