Nested Array Handling
MoreMaps.jl can map over nested array structures, applying functions at specific levels of nesting.
Leaf Types
The leaf
parameter of a Chart
determines which types are treated as non-iterable units (leaves) during mapping.
All (Default)
Treats all arrays as leaves - applies the function to the outermost array:
julia
x = [1:3, 4:6, 7:9]
C = Chart(leaf = MoreMaps.All)
y = map(sum, C, x)
3-element Vector{Int64}:
6
15
24
Union{} (Deep Mapping) {#Union
Maps over the deepest nested elements:
julia
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
C = Chart(leaf = Union{})
y = map(x -> x^2, C, x)
3-element Vector{Vector{Int64}}:
[1, 4, 9]
[16, 25, 36]
[49, 64, 81]
Specific Types
Specify a concrete type to control the mapping depth:
julia
# Nested structure: Array of Arrays of Ranges
x = [[1:2, 3:4], [5:6, 7:8]]
# Map over the ranges (innermost)
C_range = Chart(leaf = UnitRange)
y_range = map(sum, C_range, x)
# Map over the inner arrays
C_array = Chart(leaf = Vector)
y_array = map(length, C_array, x)
2-element Vector{Int64}:
2
2
Type Stability
MoreMaps maintains type stability when possible:
julia
x = randn(10)
# Type-stable with concrete leaf type
C_stable = Chart(leaf = Float64)
y = @inferred map(sqrt ∘ abs, C_stable, x)
# Type inference with Union{}
x_nested = [[1.0, 2.0], [3.0, 4.0]]
C_nested = Chart(leaf = Union{})
# Note: Union{} may not always be type-stable
y_nested = map(x -> x^2, C_nested, x_nested)
2-element Vector{Vector{Float64}}:
[1.0, 4.0]
[9.0, 16.0]
Working with Complex Structures
julia
# Mixed nested structure
data = [
[1.0:0.5:3.0, 4.0:0.5:6.0],
[7.0:0.5:9.0, 10.0:0.5:12.0]
]
# Process at different levels
C_outer = Chart(leaf = MoreMaps.All)
result_outer = map(x -> length(x), C_outer, data)
C_ranges = Chart(leaf = StepRangeLen)
result_ranges = map(x -> collect(x), C_ranges, data)
C_deep = Chart(leaf = Union{})
result_deep = map(x -> round(x), C_deep, data)
2-element Vector{Vector{Vector{Float64}}}:
[[1.0, 2.0, 2.0, 2.0, 3.0], [4.0, 4.0, 5.0, 6.0, 6.0]]
[[7.0, 8.0, 8.0, 8.0, 9.0], [10.0, 10.0, 11.0, 12.0, 12.0]]
Multiple Iterator Support
When using multiple iterators, leaf types apply to all inputs:
julia
x = [[1, 2], [3, 4]]
y = [[5, 6], [7, 8]]
C = Chart(leaf = Vector{Int})
z = map(+, C, x, y)
2-element Vector{Vector{Int64}}:
[6, 8]
[10, 12]
Performance Tips
Use concrete leaf types when possible for better type stability
MoreMaps.All
(default) is fastest for non-nested arraysUnion{}
provides maximum flexibility but may sacrifice type stabilitySpecify the most specific leaf type that matches your use case