Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add @leaf macro #55

Merged
merged 5 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
```@docs
Functors.fmap
Functors.@functor
Functors.@leaf
```

```@docs
Expand Down
16 changes: 8 additions & 8 deletions src/Functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,26 +282,26 @@ julia> struct Bar; x; end

julia> @functor Bar

julia> struct NoChildren; x; y; end
julia> struct TypeWithNoChildren; x; y; end

julia> m = Foo(Bar([1,2,3]), NoChildren(:a, :b))
Foo(Bar([1, 2, 3]), NoChildren(:a, :b))
julia> m = Foo(Bar([1,2,3]), TypeWithNoChildren(:a, :b))
Foo(Bar([1, 2, 3]), TypeWithNoChildren(:a, :b))

julia> fcollect(m)
4-element Vector{Any}:
Foo(Bar([1, 2, 3]), NoChildren(:a, :b))
Foo(Bar([1, 2, 3]), TypeWithNoChildren(:a, :b))
Bar([1, 2, 3])
[1, 2, 3]
NoChildren(:a, :b)
TypeWithNoChildren(:a, :b)

julia> fcollect(m, exclude = v -> v isa Bar)
2-element Vector{Any}:
Foo(Bar([1, 2, 3]), NoChildren(:a, :b))
NoChildren(:a, :b)
Foo(Bar([1, 2, 3]), TypeWithNoChildren(:a, :b))
TypeWithNoChildren(:a, :b)

julia> fcollect(m, exclude = v -> Functors.isleaf(v))
2-element Vector{Any}:
Foo(Bar([1, 2, 3]), NoChildren(:a, :b))
Foo(Bar([1, 2, 3]), TypeWithNoChildren(:a, :b))
Bar([1, 2, 3])
```
"""
Expand Down
24 changes: 21 additions & 3 deletions src/functor.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
function functor end

functor(T, x) = (), _ -> x
const NoChildren = Tuple{}

function makeleaf(m::Module, T)
@eval m begin
CarloLucibello marked this conversation as resolved.
Show resolved Hide resolved
$Functors.functor(::Type{<:$T}, x) = $Functors.NoChildren(), _ -> x
end
end

"""
@leaf T

Define [`functor`](@ref) for the type `T` so that `isleaf(x::T) == true`.
"""
macro leaf(T)
:(makeleaf(@__MODULE__, $(esc(T))))
end

@leaf Any # every type is a leaf by default
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
functor(::Type{<:AbstractArray{<:Number}}, x) = (), _ -> x
@leaf AbstractArray{<:Number}

function makefunctor(m::Module, T, fs = fieldnames(T))
yᵢ = 0
Expand All @@ -31,7 +49,7 @@ macro functor(args...)
functorm(args...)
end

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

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

Expand Down
13 changes: 12 additions & 1 deletion test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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) === Functors.NoChildren()
@test Functors.children(has_children) == (x=1, y=2)
end

Expand Down Expand Up @@ -352,3 +352,14 @@ end
@test fmap(+, m1, n1) == Dict("x" => [5, 7], "y" => Dict(:a=>3.1, :b=>4.2))
end
end

@testset "@leaf" begin
struct A; x; end
@functor A
a = A(1)
@test Functors.children(a) === (x = 1,)
Functors.@leaf A
children, re = Functors.functor(a)
@test children == Functors.NoChildren()
@test re(children) === a
end