Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support insert with dynamic DynamicIndexLens #132

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Accessors"
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
authors = ["Takafumi Arakaki <[email protected]>", "Jan Weidner <[email protected]> and contributors"]
version = "0.1.35"
version = "0.1.36"

[deps]
CompositionsBase = "a33af91c-f02d-484b-be07-31d278c5ca2b"
Expand Down
4 changes: 4 additions & 0 deletions src/optics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ end
delete(obj, IndexLens(lens.f(obj)))
end

Base.@propagate_inbounds function insert(obj, lens::DynamicIndexLens, x)
insert(obj, IndexLens(lens.f(obj)), x)
end


Broadcast.broadcastable(
o::Union{PropertyLens,IndexLens,DynamicIndexLens,Elements,Properties,If,Recursive}
Expand Down
5 changes: 5 additions & 0 deletions test/test_insert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ using Accessors: insert
@testset "function" begin
@test @inferred(insert( (b=2, c=3), @optic(_.a), 1 )) == (b=2, c=3, a=1)
@test insert( (b=2, c=3), @optic(_[:a]), 1 ) == (b=2, c=3, a=1)
@test insert( (2, 3), @optic(_[end]), 1) == (2, 1, 3)
let A = [1, 2]
@test insert(A, @optic(_[2]), 3) == [1, 3, 2]
@test_throws BoundsError insert(A, @optic(_[4]), 3)
@test_throws Exception insert(A, @optic(_[1, 3]), 3)
@test insert(A, first, 3) == [3, 1, 2]
@test insert(A, @optic(first(_, 2)), [3, 4]) == [3, 4, 1, 2]
@test insert(A, @optic(last(_, 2)), [3, 4]) == [1, 2, 3, 4]
@test insert(A, @optic(_[end+1]), 3) == [1, 2, 3]
@test insert(A, @optic(_[(end+1):(end+2)]), [3, 4]) == [1, 2, 3, 4]
@test A == [1, 2] # not changed
end
@test @inferred(insert(CartesianIndex(1, 2, 3), @optic(_[2]), 4)) == CartesianIndex(1, 4, 2, 3)
Expand All @@ -41,6 +44,8 @@ using Accessors: insert
@test @insert(x[(:a, :x)] = (x=:xyz, a=1)) === (b=2, c=3, a=1, x=:xyz)
x = [1, 2]
@test @insert(x[3] = 3) == [1, 2, 3]
@test @insert(x[end+1] = 3) == [1, 2, 3]
@test @insert(x[end] = 3) == [1, 3, 2]
x = (a=(b=(1, 2),), c=1)
@test @insert(x.a.b[1] = 0) == (a=(b=(0, 1, 2),), c=1)

Expand Down
Loading