Nested array replacement

Hi! I would like to model this behavior on Julia: Substitution System
So if we have rules ( 1 => [[1, 0], [1, 1]], 0 => [[0, 0], [0, 0]] )
and array [[1, 0], [1, 1]] than output should be [1, 0, 0, 0], [1, 1, 0, 0], [1, 0, 1, 0], [1, 1, 1, 1]].
How to do it with replacement and reshape?

It is a little easier if you use 2D arrays instead of vectors of vectors.

rules = Dict([1 => [1 0; 1 1], 0 => [0 0; 0 0]])

initial = [1 0; 1 1]

apply(rules, state) = reduce(
    vcat,
    map(x -> reduce(hcat, x), map(x -> getindex.(Ref(rules), x), eachrow(state))),
)

apply(rules, initial)

Thank you, cool!

julia> dm=Dict( 1 => [1 0; 1 1], 0 => [0 0; 0 0] )
Dict{Int64, Matrix{Int64}} with 2 entries:
  0 => [0 0; 0 0]
  1 => [1 0; 1 1]

julia> k=[1, 1, 0, 1]
4-element Vector{Int64}:
 1
 1
 0
 1

julia> using BlockArrays

julia> bl=get.([dm],k,missing)
4-element Vector{Matrix{Int64}}:
 [1 0; 1 1]
 [1 0; 1 1]
 [0 0; 0 0]
 [1 0; 1 1]

julia> mb=mortar(reshape(bl,2,2))
2×2-blocked 4×4 BlockMatrix{Int64}:
 1  0  │  0  0
 1  1  │  0  0
 ──────┼──────
 1  0  │  1  0
 1  1  │  1  1

julia> Matrix(mb)
4×4 Matrix{Int64}:
 1  0  0  0
 1  1  0  0
 1  0  1  0
 1  1  1  1
julia> d=Dict( 1 => [[1, 0], [1, 1]], 0 => [[0, 0], [0, 0]] )
Dict{Int64, Vector{Vector{Int64}}} with 2 entries:
  0 => [[0, 0], [0, 0]]
  1 => [[1, 0], [1, 1]]

julia> k=[[1, 0], [1, 1]]
2-element Vector{Vector{Int64}}:
 [1, 0]
 [1, 1]

julia> idx=permutedims(Tuple.(CartesianIndices((2,2,2))),(3, 1, 2))[:]
8-element Vector{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)
 (1, 1, 2)
 (2, 1, 1)
 (2, 1, 2)
 (1, 2, 1)
 (1, 2, 2)
 (2, 2, 1)
 (2, 2, 2)

julia> ridx=reshape(idx,4,2)
4×2 Matrix{Tuple{Int64, Int64, Int64}}:
 (1, 1, 1)  (1, 2, 1)
 (1, 1, 2)  (1, 2, 2)
 (2, 1, 1)  (2, 2, 1)
 (2, 1, 2)  (2, 2, 2)

julia> m=[d[k[x][y]][z] for (x,y,z) in ridx]
4×2 Matrix{Vector{Int64}}:
 [1, 0]  [0, 0]
 [1, 1]  [0, 0]
 [1, 0]  [1, 0]
 [1, 1]  [1, 1]

julia> [vcat(r...)  for r in eachrow(m)]
4-element Vector{Vector{Int64}}:
 [1, 0, 0, 0]
 [1, 1, 0, 0]
 [1, 0, 1, 0]
 [1, 1, 1, 1]