StructWalk
Documentation for StructWalk.
StructWalk.LeafNodeStructWalk.WalkStyleStructWalk.childrenStructWalk.constructorStructWalk.iscontainerStructWalk.mapleavesStructWalk.mapnonleavesStructWalk.postwalkStructWalk.prewalkStructWalk.scanStructWalk.walkstyleStructWalk.walkstyle
StructWalk.LeafNode — TypeStructWalk.WalkStyle — TypeAbstract type WalkStyle
Subtype WalkStyle and overload walkstyle to define custom walking behaviors (constructor / children /...).
StructWalk.children — Methodchildren(s::WalkStyle, x)Return the children of x, which would be feeded to constructor(s, x). If x is an container type like Array, it can return a tuple of itself and set iscontainer(s, x) to true.
See also: constructor, iscontainer
StructWalk.constructor — Methodconstructor(s::WalkStyle, x)Return the constructor for x, which would be applied to children(s, x).
See also: children, iscontainer
StructWalk.iscontainer — Methodiscontainer(s::WalkStyle, x)Return a Bool indicating whether children(x) return a tuple of itself or not.
See also: constructor, children
StructWalk.mapleaves — Methodmapleaves(f, [style = WalkStyle], x)Apply f to each leaf nodes in x and return the result. f only see leaf nodes.
Example
julia> mapleaves(x -> @show(x) isa Integer ? x + 1 : x, (a=2, b=(c=4, d=0)))
x = 2
x = 4
x = 0
(a = 3, b = (c = 5, d = 1))
StructWalk.mapnonleaves — Methodmapnonleaves(f, [style = WalkStyle], x)Apply f to each non-leaf in x and return the result. f only see non-leaf nodes.
Example
julia> StructWalk.mapnonleaves(x -> @show(x) isa Integer ? x + 1 : x, (a=2, b=(c=4, d=0)))
x = (c = 4, d = 0)
x = (a = 2, b = (c = 4, d = 0))
(a = 2, b = (c = 4, d = 0))
StructWalk.postwalk — Methodpostwalk(f, [style = WalkStyle], x)Apply f to each node in x and return the result. f sees the leaves first and then the transformed node.
Example
julia> postwalk(x -> @show(x) isa Integer ? x + 1 : x, (a=2, b=(c=4, d=0)))
x = 2
x = 4
x = 0
x = (c = 5, d = 1)
x = (a = 3, b = (c = 5, d = 1))
(a = 3, b = (c = 5, d = 1))
julia> postwalk(x -> @show(x) isa Integer ? x + 1 : x .+ 1, (3, 5))
x = 3
x = 5
x = (4, 6)
(5, 7)
julia> postwalk(x -> @show(x) isa Integer ? x // 2 : x isa Tuple ? =>(x .+ 1...) : x, (3, 5))
x = 3
x = 5
x = (3//2, 5//2)
5//2 => 7//2
See also: prewalk
StructWalk.prewalk — Methodprewalk(f, [style = WalkStyle], x)Apply f to each node in x and return the result. f sees the node first and then the transformed leaves.
Notice that it is possible it walk infinitely if you transform a node into non-leaf value. Wrapping the non-leaf value with LeafNode(y) in f to prevent infinite walk.
Example
julia> prewalk(x -> @show(x) isa Integer ? x + 1 : x, (a=2, b=(c=4, d=0)))
x = (a = 2, b = (c = 4, d = 0))
x = 2
x = (c = 4, d = 0)
x = 4
x = 0
(a = 3, b = (c = 5, d = 1))
julia> prewalk(x -> @show(x) isa Integer ? x + 1 : x .+ 1, (3, 5))
x = (3, 5)
x = 4
x = 6
(5, 7)
julia> prewalk(x -> @show(x) isa Integer ? StructWalk.LeafNode(x // 2) : x isa Tuple ? =>(x .+ 1...) : x, (3, 5))
x = (3, 5)
x = 4
x = 6
2//1 => 3//1
StructWalk.scan — Methodscan(f, [g = f, style = WalkStyle], x)Walk through x without constructing anything and applying f to leaf nodes and g to every other node.
See also mapleaves.
StructWalk.walkstyle — Functionwalkstyle(::CustomWalkStyle, x::T) where {CumstomWalkStyle <: WalkStyle}Should return a tuple of length 3 with:
1. [constructor](@ref): A proper constuctor for `T`, can be `identity` if `x` isa leaf.
2. [children](@ref): Children of `x` in a tuple, or empty tuple `()` if `x` is a leaf.
3. [iscontainer](@ref): A bool indicate whether element of 2. is the actual list of children. default to `false`.For example, since Array has 0 fieldcount, we doesn't split the value into a tuple as children. Instead, we return (x,) as children and the extra boolean true, so it will walk/map through x accordingly.
StructWalk.walkstyle — Methodwalkstyle(x)
walkstyle(::Type{WalkStyle}, x::T) where TReturn T and a tuple all field values of x. The default behavior use ConstructionBase.constructorof for the constructor and ConstructionBase.getfields for the children.