Skip to content

Commit

Permalink
Merge pull request #61 from aplavin/patch-1
Browse files Browse the repository at this point in the history
fix setting real and abs
  • Loading branch information
aplavin authored Jul 9, 2022
2 parents 28f22d0 + 9161731 commit 3864908
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
6 changes: 4 additions & 2 deletions 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.16"
version = "0.1.17"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand All @@ -22,6 +22,7 @@ InverseFunctions = "0.1.5"
MacroTools = "0.4.4, 0.5"
Requires = "0.5, 1.0"
StaticNumbers = "0.3"
Unitful = "1"
julia = "1.3"

[extras]
Expand All @@ -32,6 +33,7 @@ QuickTypes = "ae2dfa86-617c-530c-b392-ef20fdad97bb"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StaticNumbers = "c5e4b96a-f99f-5557-8ed2-dc63ef9b5131"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Test", "PerformanceTestTools", "QuickTypes", "StaticArrays", "BenchmarkTools", "InteractiveUtils", "StaticNumbers"]
test = ["Test", "PerformanceTestTools", "QuickTypes", "StaticArrays", "BenchmarkTools", "InteractiveUtils", "StaticNumbers", "Unitful"]
9 changes: 4 additions & 5 deletions src/functionlenses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ delete(path, ::typeof(dirname)) = basename(path)
################################################################################
##### math
################################################################################
set(x::Real, ::typeof(real), y) = y
set(x, ::typeof(real), y) = y + im*imag(x)
set(x, ::typeof(imag), y) = real(x) + im*y
set(x, ::typeof(angle), y) = abs(x) * cis(y)
set(x, ::typeof(abs), y) = y >= 0 ? y * sign(x) : throw(DomainError(y, "cannot set abs($x) to $y"))
set(x::T, ::typeof(real), y) where {T} = T === real(T) ? y : y + im*imag(x)
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(arr, ::typeof(normalize), val) = norm(arr) * val
set(arr, ::typeof(norm), val) = val/norm(arr) * arr # should we check val is positive?
Expand Down
6 changes: 5 additions & 1 deletion test/test_functionlenses.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TestFunctionLenses
using Test
using Dates
using Unitful
using Accessors: test_getset_laws
using Accessors

Expand Down Expand Up @@ -88,10 +89,12 @@ end
end

@testset "math" begin
x = 1
@test 2.0 === @set real(1) = 2.0
@test 2.0 + 1im === @set real(1+1im) = 2.0
@test 1.0 + 2im === @set imag(1) = 2.0
@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_getset_laws(!, true, true, false)
# no need for extensive testing: all invertible lenses are simply forwarded to InverseFunctions
Expand All @@ -102,6 +105,7 @@ end
end

x = 3 + 4im
@test @set(abs(-2u"m") = 1u"m") === -1u"m"
@test @set(abs(x) = 10) 6 + 8im
@test @set(angle(x) = π/2) 5im
@test_throws DomainError @set(abs(x) = -10)
Expand Down

4 comments on commit 3864908

@aplavin
Copy link
Member Author

@aplavin aplavin commented on 3864908 Jul 9, 2022

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/63913

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.17 -m "<description of version>" 386490861633f019486973d41a84c9a98ab657ae
git push origin v0.1.17

Also, note the warning: Version 0.1.17 skips over 0.1.15
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

@aplavin
Copy link
Member Author

@aplavin aplavin commented on 3864908 Jul 9, 2022

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 updated: JuliaRegistries/General/63913

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.17 -m "<description of version>" 386490861633f019486973d41a84c9a98ab657ae
git push origin v0.1.17

Please sign in to comment.