Skip to content

Commit

Permalink
add fld, mod, div, rem setters
Browse files Browse the repository at this point in the history
  • Loading branch information
aplavin committed Oct 28, 2022
1 parent f0a65fe commit 1139a77
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
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.20"
version = "0.1.21"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
6 changes: 5 additions & 1 deletion src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ set(x, ::typeof(imag), y) = real(x) + im*y
set(x, ::typeof(angle), y) = abs(x) * cis(y)
set(x, ::typeof(abs), y) = y >= zero(y) ? y * sign(x) : throw(DomainError(y, "cannot set abs($x) to $y"))

set(x, ::typeof(mod2pi), y) = 0 <= y <= 2π ? 2π * fld(x, 2π) + y : throw(DomainError(y, "cannot set mod2pi"))
set(x, ::typeof(mod2pi), y) = set(x, @optic(mod(_, 2π)), y)
set(x, f::Base.Fix2{typeof(fld)}, y) = set(x, @optic(first(fldmod(_, f.x))), y)
set(x, f::Base.Fix2{typeof(mod)}, y) = set(x, @optic(last(fldmod(_, f.x))), y)
set(x, f::Base.Fix2{typeof(div)}, y) = set(x, @optic(first(divrem(_, f.x))), y)
set(x, f::Base.Fix2{typeof(rem)}, y) = set(x, @optic(last(divrem(_, f.x))), y)

set(arr, ::typeof(normalize), val) = norm(arr) * val
set(arr, ::typeof(norm), val) = val/norm(arr) * arr # should we check val is positive?
Expand Down
7 changes: 6 additions & 1 deletion test/test_functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,19 @@ end
@test 1.0 + 2im === @set imag(1+1im) = 2.0
@test 1u"m" === @set real(2u"m") = 1u"m"
@test (2 + 1im)u"m" === @set imag(2u"m") = 1u"m"

@test set.(10, @optic(mod(_, 3)), 0:2) == 9:11
@test_throws DomainError set(10, @optic(mod(_, 3)), 10)

test_getset_laws(mod2pi, 5.3, 1, 2; cmp=isapprox)
test_getset_laws(mod2pi, -5.3, 1, 2; cmp=isapprox)

test_getset_laws(!, true, true, false)
@testset for o in [
# invertible lenses below: no need for extensive testing, simply forwarded to InverseFunctions
inv, +, exp, sqrt, @optic(2 + _), @optic(_ * 3), @optic(log(2, _))
inv, +, exp, sqrt, @optic(2 + _), @optic(_ * 3), @optic(log(2, _)),
# non-invertible lenses, indirectly forwarded to InverseFunctions
@optic(mod(_, 21)), @optic(fld(_, 3)), @optic(rem(_, 21)), @optic(div(_, 3)),
]
x = 5
test_getset_laws(o, x, 10, 20; cmp=isapprox)
Expand Down

2 comments on commit 1139a77

@aplavin
Copy link
Member Author

Choose a reason for hiding this comment

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

@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/71223

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.21 -m "<description of version>" 1139a77b506dc20513405adbc440acc8c3395930
git push origin v0.1.21

Please sign in to comment.