StructWalk

Documentation for StructWalk.

StructWalk.childrenMethod
children(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

source
StructWalk.mapleavesMethod
mapleaves(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))
source
StructWalk.mapnonleavesMethod
mapnonleaves(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))
source
StructWalk.postwalkMethod
postwalk(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

source
StructWalk.prewalkMethod
prewalk(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

See also: postwalk, LeafNode

source
StructWalk.scanMethod
scan(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.

source
StructWalk.walkstyleFunction
walkstyle(::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.

source
StructWalk.walkstyleMethod
walkstyle(x)
walkstyle(::Type{WalkStyle}, x::T) where T

Return T and a tuple all field values of x. The default behavior use ConstructionBase.constructorof for the constructor and ConstructionBase.getfields for the children.

source