Skip to content

Commit 1fd68e2

Browse files
committed
Rename kwarg to exclude instead of predicate. Add test for nested exclusion.
1 parent 22f2e73 commit 1fd68e2

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ It is also possible to implement `functor` by hand when greater flexibility is r
7272

7373
For a discussion regarding the need for a `cache` in the implementation of `fmap`, see [here](https://github.com/FluxML/Functors.jl/issues/2).
7474

75-
Use `predicate` for more fine-grained control over whether `fmap` descends into a particular value (the default is `predicate = Functors.isleaf`):
75+
Use `exclude` for more fine-grained control over whether `fmap` descends into a particular value (the default is `exclude = Functors.isleaf`):
7676
```julia
7777
julia> using CUDA
7878

@@ -84,7 +84,7 @@ julia> fmap(cu, x)
8484
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)
8585
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)
8686

87-
julia> fmap(cu, x; predicate = CUDA.isbitstype(eltype(x)))
87+
julia> fmap(cu, x; exclude = x -> CUDA.isbitstype(eltype(x)))
8888
3-element CuArray{Char,1}:
8989
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
9090
'b': ASCII/Unicode U+0062 (category Ll: Letter, lowercase)

src/functor.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ end
5151

5252
# See https://github.com/FluxML/Functors.jl/issues/2 for a discussion regarding the need for
5353
# cache.
54-
function fmap(f, x; predicate = isleaf, cache = IdDict())
54+
function fmap(f, x; exclude = isleaf, cache = IdDict())
5555
haskey(cache, x) && return cache[x]
56-
cache[x] = predicate(x) ? f(x) : fmap1(x -> fmap(f, x, cache = cache), x)
56+
y = exclude(x) ? f(x) : fmap1(x -> fmap(f, x, cache = cache, exclude = exclude), x)
57+
cache[x] = y
58+
59+
return y
5760
end
5861

5962
"""

test/basics.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ end
3232
@test model′.x.y isa Vector{Float64}
3333
end
3434

35-
@testset "Predicate" begin
35+
@testset "Exclude" begin
3636
f(x::AbstractArray) = x
3737
f(x::Char) = 'z'
3838

3939
x = ['a', 'b', 'c']
4040
@test fmap(f, x) == ['z', 'z', 'z']
41-
@test fmap(f, x; predicate = x -> x isa AbstractArray) == x
41+
@test fmap(f, x; exclude = x -> x isa AbstractArray) == x
42+
43+
x = (['a', 'b', 'c'], ['d', 'e', 'f'])
44+
@test fmap(f, x) == (['z', 'z', 'z'], ['z', 'z', 'z'])
45+
@test fmap(f, x; exclude = x -> x isa AbstractArray) == x
4246
end
4347

4448
@testset "Property list" begin

0 commit comments

Comments
 (0)