@@ -20,6 +20,8 @@ julia> getall(obj, @optic _ |> Elements() |> last)
20
20
"""
21
21
function getall end
22
22
23
+ # implementations for individual noncomposite optics
24
+
23
25
getall (obj:: Union{Tuple, AbstractVector} , :: Elements ) = obj
24
26
getall (obj:: Union{NamedTuple} , :: Elements ) = values (obj)
25
27
getall (obj:: AbstractArray , :: Elements ) = vec (obj)
@@ -38,8 +40,10 @@ setall(obj, o::If, vs) = error("Not supported")
38
40
setall (obj, o, vs) = set (obj, o, only (vs))
39
41
40
42
41
- # A straightforward recursive implementation of getall and setall don't actually infer,
42
- # see https://github.com/JuliaObjects/Accessors.jl/pull/64.
43
+ # implementations for composite optics
44
+
45
+ # A straightforward recursive approach doesn't actually infer,
46
+ # see https://github.com/JuliaObjects/Accessors.jl/pull/64 and https://github.com/JuliaObjects/Accessors.jl/pull/68.
43
47
# Instead, we need to generate separate functions for each recursion level.
44
48
45
49
function getall (obj, optic:: ComposedFunction )
@@ -80,6 +84,8 @@ for i in 2:10
80
84
end
81
85
82
86
87
+ # helper functions
88
+
83
89
_concat (a:: Tuple , b:: Tuple ) = (a... , b... )
84
90
_concat (a:: Tuple , b:: AbstractVector ) = vcat (collect (a), b)
85
91
_concat (a:: AbstractVector , b:: Tuple ) = vcat (a, collect (b))
@@ -93,10 +99,6 @@ _reduce_concat(xs::AbstractVector{<:AbstractVector}) = reduce(vcat, xs)
93
99
_staticlength (:: NTuple{N, <:Any} ) where {N} = Val (N)
94
100
_staticlength (x:: AbstractVector ) = length (x)
95
101
96
- _val (N:: Int ) = N
97
- _val (:: Val{N} ) where {N} = N
98
-
99
-
100
102
getall_lengths (obj, optic, :: Val{1} ) = _staticlength (getall (obj, optic))
101
103
for i in 2 : 10
102
104
@eval function getall_lengths (obj, optic, :: Val{$i} )
@@ -107,12 +109,10 @@ for i in 2:10
107
109
end
108
110
end
109
111
110
-
111
112
nestedsum (ls:: Int ) = ls
112
113
nestedsum (ls:: Val ) = ls
113
114
nestedsum (ls:: Tuple ) = sum (_val ∘ nestedsum, ls)
114
115
115
-
116
116
to_nested_shape (vs, :: Val{LS} , :: Val{1} ) where {LS} = (@assert length (vs) == _val (LS); vs)
117
117
for i in 2 : 10
118
118
@eval @generated function to_nested_shape (vs, ls:: Val{LS} , :: Val{$i} ) where {LS}
@@ -129,3 +129,6 @@ for i in 2:10
129
129
:( ($ (subs... ),) )
130
130
end
131
131
end
132
+
133
+ _val (N:: Int ) = N
134
+ _val (:: Val{N} ) where {N} = N
0 commit comments