Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CarloLucibello committed Nov 27, 2022
1 parent 40b77a8 commit 81017f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
24 changes: 9 additions & 15 deletions src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@ functor(::Type{<:AbstractArray}, x) = x, identity
functor(::Type{<:AbstractArray{<:Number}}, x) = (), _ -> x

function makefunctor(m::Module, T, fs = fieldnames(T))
if isempty(fs)
@eval m begin
$Functors.functor(::Type{<:$T}, x) = (), _ -> x
end
else
yᵢ = 0
escargs = map(fieldnames(T)) do f
f in fs ? :(y[$(yᵢ += 1)]) : :(x.$f)
end
escfs = [:($f=x.$f) for f in fs]

@eval m begin
$Functors.functor(::Type{<:$T}, x) = (;$(escfs...)), y -> $T($(escargs...))
end
yᵢ = 0
escargs = map(fieldnames(T)) do f
f in fs ? :(y[$(yᵢ += 1)]) : :(x.$f)
end
escfs = [:($f=x.$f) for f in fs]

@eval m begin
$Functors.functor(::Type{<:$T}, x) = (;$(escfs...)), y -> $T($(escargs...))
end
end

Expand All @@ -44,7 +38,7 @@ macro functor(args...)
functorm(args...)
end

isleaf(@nospecialize(x)) = children(x) === ()
isleaf(@nospecialize(x)) = isempty(children(x))

children(x) = functor(x)[1]

Expand Down
2 changes: 1 addition & 1 deletion test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct NoChild{T}; x::T; end
has_children = Foo(1, 2)
@test Functors.isleaf(no_children)
@test !Functors.isleaf(has_children)
@test Functors.children(no_children) == ()
@test Functors.children(no_children) == (;)
@test Functors.children(has_children) == (x=1, y=2)
end

Expand Down

0 comments on commit 81017f2

Please sign in to comment.