diff --git a/Project.toml b/Project.toml index 1bca272..44e2a3d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Functors" uuid = "d9f16b24-f501-4c13-a1f2-28368ffc5196" authors = ["Mike J Innes "] -version = "0.5.2" +version = "0.5.3" [deps] Compat = "34da2185-b29b-5c13-b0c7-acf172513d20" diff --git a/src/base.jl b/src/base.jl index 7b8a3ad..0798a61 100644 --- a/src/base.jl +++ b/src/base.jl @@ -34,6 +34,8 @@ end functor(::Type{<:Base.Fix{N}}, x) where N = (; x.f, x.x), y -> Base.Fix{N}(y.f, y.x) end +# Otherwise Base.Set are treated as Dict{K,Nothing} +functor(::Type{S}, x) where {S<:AbstractSet} = collect(x), constructorof(S) ### ### Array wrappers diff --git a/test/base.jl b/test/base.jl index 918401e..d223aca 100644 --- a/test/base.jl +++ b/test/base.jl @@ -49,6 +49,14 @@ end @test fmap(sqrt, Base.Fix2(/, 4); exclude)(10) == 5.0 end +@testset "Set" begin + s = Set([4, 9]) + (xs, rec) = Functors.functor(s) + @test issetequal(xs, s) + @test rec(xs) == s + @test fmap(sqrt, s) == rec(map(sqrt, xs)) == Set([2, 3]) +end + @testset "BroadcastFunction" begin f = Bar(3.3) bf = Base.Broadcast.BroadcastFunction(f) @@ -213,6 +221,15 @@ end @test od2[2] == 4 end +@testset "AbstractSet is functor" begin + os = OrderedSet([1, 2]) + @test !Functors.isleaf(os) + os2 = fmap(x -> 2x, os) + @test os2 isa OrderedSet + @test os2[1] == 2 + @test os2[2] == 4 +end + @testset "Types are leaves" begin @test Functors.isleaf(Int) @test Functors.isleaf(Array) diff --git a/test/runtests.jl b/test/runtests.jl index 670db20..cf242cb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,7 +2,7 @@ using Functors, Test using Zygote using LinearAlgebra using StaticArrays -using OrderedCollections: OrderedDict +using OrderedCollections: OrderedDict, OrderedSet using Measurements: ± @testset "Functors.jl" begin