Skip to content

Commit

Permalink
fix FluxML#16
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson committed Oct 5, 2021
1 parent 059a5dc commit 63de781
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/functor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,12 @@ julia> fcollect(m, exclude = v -> Functors.isleaf(v))
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
function fcollect(x; output = [], cache = Base.IdSet(), exclude = v -> false)
x in cache && return output
if !exclude(x)
push!(cache, x)
push!(output, x)
foreach(y -> fcollect(y; cache=cache, output=output, exclude=exclude), children(x))
end
return output
end
5 changes: 5 additions & 0 deletions test/basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ end
m3 = Foo(m2, m0)
m4 = Bar(m3)
@test all(fcollect(m4) .=== [m4, m3, m2, m1, m0])

m1 = [1, 2, 3]
m2 = [1, 2, 3]
m3 = Foo(m1, m2)
@test all(fcollect(m3) .=== [m3, m1, m2])
end

struct FFoo
Expand Down

0 comments on commit 63de781

Please sign in to comment.