Skip to content

Commit

Permalink
numbers are leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLucibello committed Oct 21, 2024
1 parent 430844d commit 3c76a03
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/base.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

functor(::Type{<:Base.ComposedFunction}, x) = (outer = x.outer, inner = x.inner), y -> Base.ComposedFunction(y.outer, y.inner)
@leaf Number

functor(::Type{<:Tuple}, x) = x, identity
functor(::Type{<:NamedTuple{L}}, x) where L = NamedTuple{L}(map(s -> getproperty(x, s), L)), identity
functor(::Type{<:Dict}, x) = Dict(k => x[k] for k in keys(x)), identity

functor(::Type{<:AbstractArray}, x) = x, identity
@leaf AbstractArray{<:Number}


###
### Array wrappers
Expand Down
7 changes: 0 additions & 7 deletions src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@ end

functor(x) = functor(typeof(x), x)

functor(::Type{<:Tuple}, x) = x, identity
functor(::Type{<:NamedTuple{L}}, x) where L = NamedTuple{L}(map(s -> getproperty(x, s), L)), identity
functor(::Type{<:Dict}, x) = Dict(k => x[k] for k in keys(x)), identity

functor(::Type{<:AbstractArray}, x) = x, identity
@leaf AbstractArray{<:Number}

function makefunctor(m::Module, T, fs = fieldnames(T))
fidx = Ref(0)
escargs = map(fieldnames(T)) do f
Expand Down
7 changes: 7 additions & 0 deletions test/base.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
@testset "Numbers are leaves" begin
@test Functors.isleaf(1)
@test Functors.isleaf(1.0)
@test Functors.isleaf(1im)
@test Functors.isleaf(1//2)
@test Functors.isleaf(1.0 + 2.0im)
end

@testset "RefValue" begin
@test fmap(sqrt, Ref(16))[] == 4.0
Expand Down

0 comments on commit 3c76a03

Please sign in to comment.