Skip to content

Commit 1139a77

Browse files
committed
add fld, mod, div, rem setters
1 parent f0a65fe commit 1139a77

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Accessors"
22
uuid = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
33
authors = ["Takafumi Arakaki <[email protected]>", "Jan Weidner <[email protected]> and contributors"]
4-
version = "0.1.20"
4+
version = "0.1.21"
55

66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

src/functionlenses.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ set(x, ::typeof(imag), y) = real(x) + im*y
5353
set(x, ::typeof(angle), y) = abs(x) * cis(y)
5454
set(x, ::typeof(abs), y) = y >= zero(y) ? y * sign(x) : throw(DomainError(y, "cannot set abs($x) to $y"))
5555

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

5862
set(arr, ::typeof(normalize), val) = norm(arr) * val
5963
set(arr, ::typeof(norm), val) = val/norm(arr) * arr # should we check val is positive?

test/test_functionlenses.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,19 @@ end
9595
@test 1.0 + 2im === @set imag(1+1im) = 2.0
9696
@test 1u"m" === @set real(2u"m") = 1u"m"
9797
@test (2 + 1im)u"m" === @set imag(2u"m") = 1u"m"
98+
99+
@test set.(10, @optic(mod(_, 3)), 0:2) == 9:11
100+
@test_throws DomainError set(10, @optic(mod(_, 3)), 10)
98101

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

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

0 commit comments

Comments
 (0)