@@ -39,11 +39,15 @@ function _default_walk(f, x)
3939 re (map (f, func))
4040end
4141
42+ usecache (x) = ! isbits (x)
43+
4244struct NoKeyword end
4345
44- function fmap (f, x; exclude = isleaf, walk = _default_walk, cache = IdDict (), prune = NoKeyword ())
45- haskey (cache, x) && return prune isa NoKeyword ? cache[x] : prune
46- cache[x] = exclude (x) ? f (x) : walk (x -> fmap (f, x; exclude= exclude, walk= walk, cache= cache, prune= prune), x)
46+ function fmap (f, x; exclude = isleaf, walk = _default_walk, cache = usecache (x) ? IdDict () : nothing , prune = NoKeyword ())
47+ usecache (x) && haskey (cache, x) && return prune isa NoKeyword ? cache[x] : prune
48+ xnew = exclude (x) ? f (x) : walk (x -> fmap (f, x; exclude= exclude, walk= walk, cache= cache, prune= prune), x)
49+ usecache (x) && setindex! (cache, xnew, x)
50+ return xnew
4751end
4852
4953# ##
7074# ##
7175
7276function fmap (f, x, ys... ; exclude = isleaf, walk = _default_walk, cache = IdDict (), prune = NoKeyword ())
73- haskey (cache, x) && return prune isa NoKeyword ? cache[x] : prune
74- cache[x] = exclude (x) ? f (x, ys... ) : walk ((xy... ,) -> fmap (f, xy... ; exclude= exclude, walk= walk, cache= cache, prune= prune), x, ys... )
77+ usecache (x) && haskey (cache, x) && return prune isa NoKeyword ? cache[x] : prune
78+ xnew = exclude (x) ? f (x, ys... ) : walk ((xy... ,) -> fmap (f, xy... ; exclude= exclude, walk= walk, cache= cache, prune= prune), x, ys... )
79+ usecache (x) && setindex! (cache, xnew, x)
80+ return xnew
7581end
7682
7783function _default_walk (f, x, ys... )
0 commit comments