Skip to content

Commit 53311f1

Browse files
committed
simplify getall
1 parent 9ed3188 commit 53311f1

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

src/getsetall.jl

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ setall(obj, o, vs) = set(obj, o, only(vs))
4343
# Instead, we need to generate unrolled code explicitly.
4444
function getall(obj, optic::ComposedFunction)
4545
N = length(decompose(optic))
46-
_GetAll{N}()(obj, optic)
46+
_getall(obj, optic, Val(N))
4747
end
4848

4949
function setall(obj, optic::ComposedFunction, vs)
@@ -53,8 +53,8 @@ function setall(obj, optic::ComposedFunction, vs)
5353
end
5454

5555

56-
struct _GetAll{N} end
57-
(::_GetAll{N})(_) where {N} = error("Too many chained optics: $N is not supported for now. See also https://github.com/JuliaObjects/Accessors.jl/pull/64.")
56+
_getall(_, _, ::Val{N}) where {N} = error("Too many chained optics: $N is not supported for now. See also https://github.com/JuliaObjects/Accessors.jl/pull/64.")
57+
_setall(_, _, _, ::Val{N}) where {N} = error("Too many chained optics: $N is not supported for now. See also https://github.com/JuliaObjects/Accessors.jl/pull/68.")
5858

5959
_concat(a::Tuple, b::Tuple) = (a..., b...)
6060
_concat(a::Tuple, b::AbstractVector) = vcat(collect(a), b)
@@ -66,28 +66,16 @@ _reduce_concat(xs::AbstractVector) = reduce(append!, xs; init=eltype(eltype(xs))
6666
_reduce_concat(xs::Tuple{AbstractVector, Vararg{AbstractVector}}) = reduce(vcat, xs)
6767
_reduce_concat(xs::AbstractVector{<:AbstractVector}) = reduce(vcat, xs)
6868

69-
function _generate_getall(N::Int)
70-
syms = [Symbol(:f_, i) for i in 1:N]
71-
72-
expr = :( getall(obj, $(syms[end])) )
73-
for s in syms[1:end - 1] |> reverse
74-
expr = :(
75-
_reduce_concat(
76-
map(getall(obj, $(s))) do obj
77-
$expr
78-
end
79-
)
80-
)
81-
end
82-
83-
:(function (::_GetAll{$N})(obj, optic)
84-
($(syms...),) = deopcompose(optic)
85-
$expr
86-
end)
87-
end
8869

70+
_getall(obj, optic, ::Val{1}) = getall(obj, optic)
8971
for i in 2:10
90-
eval(_generate_getall(i))
72+
@eval function _getall(obj, optic, ::Val{$i})
73+
_reduce_concat(
74+
map(getall(obj, optic.inner)) do obj
75+
_getall(obj, optic.outer, Val($(i-1)))
76+
end
77+
)
78+
end
9179
end
9280

9381

@@ -102,7 +90,7 @@ _val(::Type{Val{N}}) where {N} = N
10290

10391
getall_lengths(obj, optic, ::Val{1}) = _staticlength(getall(obj, optic))
10492
for i in 2:10
105-
@eval function getall_lengths(obj, optic::ComposedFunction, ::Val{$i})
93+
@eval function getall_lengths(obj, optic, ::Val{$i})
10694
map(getall(obj, optic.inner)) do o
10795
getall_lengths(o, optic.outer, Val($(i - 1)))
10896
end
@@ -135,7 +123,7 @@ end
135123

136124
_setall(obj, optic, vs, ::Val{1}) = setall(obj, optic, vs)
137125
for i in 2:10
138-
@eval function _setall(obj, optic::ComposedFunction, vs, ::Val{$i})
126+
@eval function _setall(obj, optic, vs, ::Val{$i})
139127
setall(obj, optic.inner, map(getall(obj, optic.inner), vs) do obj, vss
140128
_setall(obj, optic.outer, vss, Val($(i - 1)))
141129
end)

0 commit comments

Comments
 (0)