Skip to content

add fcollect #13

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

Merged
merged 10 commits into from
Feb 27, 2021
Merged
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 src/Functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Functors

using MacroTools

export @functor, fmap
export @functor, fmap, fcollect

include("functor.jl")

Expand Down
70 changes: 69 additions & 1 deletion src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,20 @@ macro functor(args...)
functorm(args...)
end

isleaf(x) = functor(x)[1] === ()
"""
isleaf(x)

Return true if `x` has no [`children`](@ref) according to [`functor`](@ref).
"""
isleaf(x) = children(x) === ()

"""
children(x)

Return the children of `x` as defined by [`functor`](@ref).
Equivalent to `functor(x)[1]`.
"""
children(x) = functor(x)[1]

function fmap1(f, x)
func, re = functor(x)
Expand All @@ -42,3 +55,58 @@ function fmap(f, x; cache = IdDict())
haskey(cache, x) && return cache[x]
cache[x] = isleaf(x) ? f(x) : fmap1(x -> fmap(f, x, cache = cache), x)
end

"""
fcollect(x; exclude = v -> false)

Traverse `x` by recursing each child of `x` as defined by [`functor`](@ref)
and collecting the results into a flat array.

Doesn't recurse inside branches rooted at nodes `v`
for which `exclude(v) == true`.
In such cases, the root `v` is also excluded from the result.
By default, `exclude` always yields `false`.

See also [`children`](@ref).

# Examples

```jldoctest
julia> struct Foo; x; y; end

julia> @functor Foo

julia> struct Bar; x; end

julia> @functor Bar

julia> struct NoChildren; x; y; end

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

julia> fcollect(m)
4-element Vector{Any}:
Foo(Bar([1, 2, 3]), NoChildren(:a, :b))
Bar([1, 2, 3])
[1, 2, 3]
NoChildren(: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)

julia> fcollect(m, exclude = v -> Functors.isleaf(v))
2-element Vector{Any}:
Foo(Bar([1, 2, 3]), NoChildren(:a, :b))
Bar([1, 2, 3])
```
"""
function fcollect(x; cache = [], exclude = v -> false)
x in cache && return cache
if !exclude(x)
push!(cache, x)
foreach(y -> fcollect(y; cache = cache, exclude = exclude), children(x))
end
return cache
end
58 changes: 39 additions & 19 deletions test/basics.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
using Functors, Test

@testset "Nested" begin
struct Foo
x
y
end
struct Foo
x
y
end
@functor Foo

@functor Foo
struct Bar
x
end
@functor Bar

struct Bar
x
end
struct Baz
x
y
z
end
@functor Baz (y,)

@functor Bar
struct NoChildren
x
y
end

@testset "Nested" begin
model = Bar(Foo(1, [1, 2, 3]))

model′ = fmap(float, model)
Expand All @@ -22,17 +32,27 @@ using Functors, Test
@test model′.x.y isa Vector{Float64}
end

@testset "Property list" begin
struct Baz
x
y
z
end

@functor Baz (y,)

@testset "Property list" begin
model = Baz(1, 2, 3)
model′ = fmap(x -> 2x, model)

@test (model′.x, model′.y, model′.z) == (1, 4, 3)
end

@testset "fcollect" begin
m1 = [1, 2, 3]
m2 = 1
m3 = Foo(m1, m2)
m4 = Bar(m3)
@test all(fcollect(m4) .=== [m4, m3, m1, m2])
@test all(fcollect(m4, exclude = x -> x isa Array) .=== [m4, m3, m2])
@test all(fcollect(m4, exclude = x -> x isa Foo) .=== [m4])

m1 = [1, 2, 3]
m2 = Bar(m1)
m0 = NoChildren(:a, :b)
m3 = Foo(m2, m0)
m4 = Bar(m3)
println(fcollect(m4))
@test all(fcollect(m4) .=== [m4, m3, m2, m1, m0])
end