Skip to content

Commit 9ea11f1

Browse files
authored
Merge pull request #65 from gaurav-arya/ag-fixapi
2 parents 52cbbc8 + 8eca7c0 commit 9ea11f1

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Functors.isleaf
1212

1313
```@docs
1414
Functors.AbstractWalk
15+
Functors.execute
1516
Functors.DefaultWalk
1617
Functors.StructuralWalk
1718
Functors.ExcludeWalk

src/Functors.jl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Functors
22

3-
export @functor, @flexiblefunctor, fmap, fmapstructure, fcollect
3+
export @functor, @flexiblefunctor, fmap, fmapstructure, fcollect, execute
44

55
include("functor.jl")
66
include("walks.jl")
@@ -105,7 +105,6 @@ children
105105

106106
"""
107107
fmap(f, x, ys...; exclude = Functors.isleaf, walk = Functors.DefaultWalk()[, prune])
108-
fmap(walk, f, x, ys...)
109108
110109
A structure and type preserving `map`.
111110
@@ -190,10 +189,10 @@ use [`fmapstructure`](@ref).
190189
191190
For advanced customization of the traversal behaviour,
192191
pass a custom `walk` function that subtypes [`Functors.AbstractWalk`](ref).
193-
The form `fmap(walk, f, x, ys...)` can be called for custom walks.
194-
The simpler form `fmap(f, x, ys...; walk = mywalk)` will wrap `mywalk` in
192+
The call `fmap(f, x, ys...; walk = mywalk)` will wrap `mywalk` in
195193
[`ExcludeWalk`](@ref) then [`CachedWalk`](@ref).
196-
194+
Here, [`ExcludeWalk`](@ref) is responsible for applying `f` at excluded nodes.
195+
For a low-level interface for executing a user-constructed walk, see [`execute`](@ref).
197196
```jldoctest withfoo
198197
julia> struct MyWalk <: Functors.AbstractWalk end
199198
@@ -202,9 +201,6 @@ julia> (::MyWalk)(recurse, x) = x isa Bar ? "hello" :
202201
203202
julia> fmap(x -> 10x, m; walk = MyWalk())
204203
Foo("hello", (40, 50, "hello"))
205-
206-
julia> fmap(MyWalk(), x -> 10x, m)
207-
Foo("hello", (4, 5, "hello"))
208204
```
209205
210206
The behaviour when the same node appears twice can be altered by giving a value

src/maps.jl

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
# Note that the argument f is not actually used in this method.
2-
# See issue #62 for a discussion on how best to remove it.
3-
function fmap(walk::AbstractWalk, f, x, ys...)
4-
# This avoids a performance penalty for recursive constructs in an anonymous function.
5-
# See Julia issue #47760 and Functors.jl issue #59.
6-
recurse(xs...) = walk(var"#self#", xs...)
7-
walk(recurse, x, ys...)
8-
end
1+
Base.@deprecate fmap(walk::AbstractWalk, f, x, ys...) execute(walk, x, ys...)
92

103
function fmap(f, x, ys...; exclude = isleaf,
114
walk = DefaultWalk(),
@@ -15,10 +8,10 @@ function fmap(f, x, ys...; exclude = isleaf,
158
if !isnothing(cache)
169
_walk = CachedWalk(_walk, prune, cache)
1710
end
18-
fmap(_walk, f, x, ys...)
11+
execute(_walk, x, ys...)
1912
end
2013

2114
fmapstructure(f, x; kwargs...) = fmap(f, x; walk = StructuralWalk(), kwargs...)
2215

2316
fcollect(x; exclude = v -> false) =
24-
fmap(ExcludeWalk(CollectWalk(), _ -> nothing, exclude), _ -> nothing, x)
17+
execute(ExcludeWalk(CollectWalk(), _ -> nothing, exclude), x)

src/walks.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ The choice of which nodes to recurse and in what order is custom to the walk.
2424
"""
2525
abstract type AbstractWalk end
2626

27+
"""
28+
execute(walk, x, ys...)
29+
30+
Execute a `walk` that recursively calls itself, starting at a node `x` in a Functors tree,
31+
as well as optional associated nodes `ys...` in other Functors trees.
32+
Any custom `walk` function that subtypes [`Functors.AbstractWalk`](@ref) is permitted.
33+
"""
34+
function execute(walk::AbstractWalk, x, ys...)
35+
# This avoids a performance penalty for recursive constructs in an anonymous function.
36+
# See Julia issue #47760 and Functors.jl issue #59.
37+
recurse(xs...) = walk(var"#self#", xs...)
38+
walk(recurse, x, ys...)
39+
end
40+
2741
"""
2842
AnonymousWalk(walk_fn)
2943

test/basics.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,7 @@ end
378378
@test zipped_iter isa Iterators.Flatten
379379
@test collect(zipped_iter) == collect(Iterators.zip([1, 2, 3, 4, 5, 6, 7, 8].^2, [8, 7, 6, 5, 4, 3, 2, 1].^2))
380380
end
381+
382+
@testset "Deprecated first-arg walk API to fmap" begin
383+
@test (@test_deprecated fmap(Functors.DefaultWalk(), nothing, (1, 2, 3))) == (1, 2, 3)
384+
end

0 commit comments

Comments
 (0)