Skip to content

Commit

Permalink
Merge pull request #45 from aplavin/master
Browse files Browse the repository at this point in the history
support two-argument functions in optics
  • Loading branch information
jw3126 authored Feb 11, 2022
2 parents fb8fb82 + 4e0af58 commit 5854cda
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
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 <aka.tkf@gmail.com>", "Jan Weidner <jw3126@gmail.com> and contributors"]
version = "0.1.8"
version = "0.1.9"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
2 changes: 2 additions & 0 deletions src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ delete(obj, ::typeof(first)) = delete(obj, IndexLens((firstindex(obj),)))
insert(obj, ::typeof(last), val) = insert(obj, IndexLens((lastindex(obj) + 1,)), val)
insert(obj, ::typeof(first), val) = insert(obj, IndexLens((firstindex(obj),)), val)

delete(obj, o::Base.Fix2{typeof(first)}) = obj[(firstindex(obj) + o.x):end]

set(obj, ::typeof(identity), val) = val
set(obj, ::typeof(inv), new_inv) = inv(new_inv)

Expand Down
18 changes: 18 additions & 0 deletions src/sugar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,24 @@ function parse_obj_optics(ex)
elseif @capture(ex, f_(front_))
obj, frontoptic = parse_obj_optics(front)
optic = esc(f) # function optic
elseif @capture(ex, f_(args__))
args_contain_under = map(args) do arg
foldtree((yes, x) -> yes || x === :_, false, arg)
end
if !any(args_contain_under)
# as if f(args...) didn't match
obj = esc(ex)
return obj, ()
end
length(args) == 2 || error("Only 1- and 2-argument functions are supported")
sum(args_contain_under) == 1 || error("Only a single function argument can be the optic target")
if args_contain_under[1]
obj, frontoptic = parse_obj_optics(args[1])
optic = :(Base.Fix2($(esc(f)), $(esc(args[2]))))
elseif args_contain_under[2]
obj, frontoptic = parse_obj_optics(args[2])
optic = :(Base.Fix1($(esc(f)), $(esc(args[1]))))
end
else
obj = esc(ex)
return obj, ()
Expand Down
5 changes: 5 additions & 0 deletions test/test_delete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ using StaticArrays
@test delete( (1,2,3), last ) == (1, 2)
@test @inferred(delete( (a=1, b=2, c=3), first ))== (b=2, c=3)
@test @inferred(delete( SVector(1,2,3), last )) === SVector(1, 2)

l = @optic first(_, 2)
VERSION >= v"1.4" && @test l((1,2,3)) == [1,2]
@test delete((1,2,3), l) === (3,)

let A = [1,2,3]
@test delete(A, @optic(_[2])) == [1, 3]
@test_throws Exception delete(A, @optic(_[2, 2]))
Expand Down
16 changes: 16 additions & 0 deletions test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,20 @@ end

end

@testset "custom binary function" begin
(x, y) = x - y
Accessors.set(x, f::Base.Fix1{typeof(↑)}, y) = f.x - y
Accessors.set(x, f::Base.Fix2{typeof(↑)}, y) = f.x + y

x = 5
o1 = @optic 2 _
o2 = @optic _ 2
@test o1(x) == -3
@test set(x, o1, 10) == -8
@test o2(x) == 3
@test set(x, o2, 10) == 12
test_getset_laws(o1, x, 2, -3)
test_getset_laws(o2, x, 2, -3)
end

end # module

2 comments on commit 5854cda

@jw3126
Copy link
Member Author

@jw3126 jw3126 commented on 5854cda Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/54430

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.9 -m "<description of version>" 5854cda8de160f2d7a9fba164eac3b7f1c13daca
git push origin v0.1.9

Please sign in to comment.