Skip to content

Commit 79ee865

Browse files
authored
improve getall/setall for staticarrays (#108)
* getall() on static arrays returns tuples * fix hetero tuples * keep static arrays in setall() * fix tests on 1.9- * test different eltypes
1 parent 29a4537 commit 79ee865

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

Project.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ AxisKeys = "0.1,0.2" # extension tests only pass for AxisKeys 0.2, but 0.1 comp
3131
Compat = "3.18, 4"
3232
CompositionsBase = "0.1"
3333
ConstructionBase = "1.5"
34+
ConstructionBaseExtras = "0.1"
3435
IntervalSets = "0.5,0.7" # extension tests only pass for IntervalSets 0.7, but 0.5 compat is required to run tests on Julia 1.3
3536
InverseFunctions = "0.1.5"
3637
MacroTools = "0.4.4, 0.5"
@@ -44,6 +45,7 @@ julia = "1.3"
4445
[extras]
4546
AxisKeys = "94b1ba4f-4ee9-5380-92f1-94cde586c3c5"
4647
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
48+
ConstructionBaseExtras = "914cd950-b775-4282-9f32-54fc4544c321"
4749
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
4850
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
4951
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
@@ -57,4 +59,5 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
5759
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
5860

5961
[targets]
60-
test = ["Test", "PerformanceTestTools", "QuickTypes", "InverseFunctions", "AxisKeys", "IntervalSets", "StaticArrays", "StructArrays", "BenchmarkTools", "InteractiveUtils", "StaticNumbers", "Unitful","Aqua"]
62+
test = ["Test", "PerformanceTestTools", "QuickTypes", "InverseFunctions", "AxisKeys", "IntervalSets", "StaticArrays", "StructArrays", "BenchmarkTools", "InteractiveUtils", "StaticNumbers", "Unitful", "Aqua", "ConstructionBaseExtras"]
63+

ext/AccessorsStaticArraysExt.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ import Accessors: setindex, delete, insert
1010

1111
Accessors.set(obj::StaticArrays.SVector, ::Type{Tuple}, val::Tuple) = StaticArrays.SVector(val)
1212

13+
Accessors.getall(obj::StaticArrays.StaticArray, ::Elements) = Tuple(obj)
14+
Accessors.setall(obj::StaticArrays.StaticArray, ::Elements, vs::AbstractArray) = constructorof(typeof(obj))(vs...) # just for disambiguation
15+
Accessors.setall(obj::StaticArrays.StaticArray, ::Elements, vs) = constructorof(typeof(obj))(vs...)
16+
1317
end

src/getsetall.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ _reduce_concat(xs::AbstractVector) = reduce(append!, xs; init=eltype(eltype(xs))
135135
_reduce_concat(xs::Tuple{AbstractVector, Vararg{AbstractVector}}) = reduce(vcat, xs)
136136
_reduce_concat(xs::AbstractVector{<:AbstractVector}) = reduce(vcat, xs)
137137

138-
_staticlength(::NTuple{N, <:Any}) where {N} = Val(N)
138+
_staticlength(::NTuple{N, Any}) where {N} = Val(N)
139139
_staticlength(x::AbstractVector) = length(x)
140140

141141
getall_lengths(obj, optics::Tuple{Any}) = _staticlength(getall(obj, only(optics)))

test/test_getsetall.jl

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ using Accessors
44
using StaticNumbers
55
using StaticArrays
66

7+
@static if VERSION < v"1.9-"
8+
# for StaticArrays constructorof: ConstructionBase itself only supports it through 1.9 extension
9+
using ConstructionBaseExtras
10+
end
711

812
if VERSION >= v"1.6" # for ComposedFunction
913
@testset "getall" begin
@@ -37,17 +41,16 @@ if VERSION >= v"1.6" # for ComposedFunction
3741
@test (2, 5, 10, 17, 26, 37) === @inferred getall(obj, @optic _ |> _[:] |> Elements() |> Elements() |> _[:] |> Elements() |> Elements() |> _[1]^2 + 1 |> only)
3842

3943
# trickier types for Elements():
40-
obj = (a=("ab", "c"), b=([1 2; 3 4],), c=(SVector(1), SVector(2, 3)))
44+
obj = (a=("ab", "c"), b=([1 2; 3 4],), c=(SVector(1.), SVector(2, 3)))
4145
@test ['b', 'c', 'd'] == @inferred getall(obj, @optic _.a |> Elements() |> Elements() |> _ + 1)
4246
@test [2, 4, 3, 5] == @inferred getall(obj, @optic _.b |> Elements() |> Elements() |> _ + 1)
43-
@test SVector(1, 2, 3) === @inferred getall(obj, @optic _.c |> Elements() |> Elements())
44-
@test [2, 3, 4] == @inferred getall(obj, @optic _.c |> Elements() |> Elements() |> _ + 1)
45-
@test_broken SVector(2, 3, 4) === getall(obj, @optic _.c |> Elements() |> Elements() |> _ + 1)
47+
@test (1., 2, 3) === @inferred getall(obj, @optic _.c |> Elements() |> Elements())
48+
@test (2., 3, 4) === @inferred getall(obj, @optic _.c |> Elements() |> Elements() |> _ + 1)
4649

4750
# composition order should not matter:
48-
@test [2, 3, 4] == @inferred getall(obj, (@optic(_ + 1) Elements() Elements()) @optic(_.c))
49-
@test [2, 3, 4] == @inferred getall(obj, (@optic(_ + 1) Elements()) (Elements() @optic(_.c)))
50-
@test [2, 3, 4] == @inferred getall(obj, @optic(_ + 1) (Elements() Elements() @optic(_.c)))
51+
@test (2., 3, 4) === @inferred getall(obj, (@optic(_ + 1) Elements() Elements()) @optic(_.c))
52+
@test (2., 3, 4) === @inferred getall(obj, (@optic(_ + 1) Elements()) (Elements() @optic(_.c)))
53+
@test (2., 3, 4) === @inferred getall(obj, @optic(_ + 1) (Elements() Elements() @optic(_.c)))
5154

5255
obj = ()
5356
@test () === @inferred getall(obj, @optic _ |> Elements() |> _ + 1)
@@ -114,6 +117,20 @@ end
114117
@test (a=1, b=((c=-3., d=-4.), (c=-5., d=-6.))) === @inferred setall(obj, (@optic(_ * 3) Properties()) (Elements() @optic(_.b)), [-9, -12, -15, -18])
115118
@test (a=1, b=((c=-3., d=-4.), (c=-5., d=-6.))) === @inferred setall(obj, @optic(_ * 3) (Properties() Elements() @optic(_.b)), [-9, -12, -15, -18])
116119

120+
# SVectors and nested Elements:
121+
obj = (c=(SVector(1.), SVector(2, 3)),)
122+
@test setall(obj.c[1], Elements(), (5, 6)) === SVector(5, 6)
123+
@test setall(obj.c[1], Elements(), (5,)) === SVector(5)
124+
@test setall(obj.c[1], Elements(), [5, 6]) === SVector(5, 6)
125+
@test setall(obj.c[1], Elements(), [5]) === SVector(5)
126+
@testset for o in (
127+
(@optic _.c |> Elements() |> Elements()),
128+
(@optic _.c |> Elements() |> Elements() |> _ + 1),
129+
)
130+
@test setall(obj, o, getall(obj, o)) === obj
131+
@test setall(obj, o, collect(getall(obj, o))) === obj
132+
end
133+
117134
obj = ([1, 2], 3:5, (6,))
118135
@test obj == setall(obj, @optic(_ |> Elements() |> Elements()), 1:6)
119136
@test ([2, 3], 4:6, (7,)) == setall(obj, @optic(_ |> Elements() |> Elements() |> _ - 1), 1:6)

0 commit comments

Comments
 (0)