Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Functors"
uuid = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
authors = ["Mike J Innes <mike.j.innes@gmail.com>"]
version = "0.5.2"
version = "0.5.3"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
2 changes: 2 additions & 0 deletions src/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading