Skip to content

Commit

Permalink
templete with deep nested input
Browse files Browse the repository at this point in the history
  • Loading branch information
chengchingwen committed Jan 8, 2023
1 parent d0d109c commit 92f77ed
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ using FuncPipelines: FixRest

using Base.PCRE

isnestedconcretetype(_) = true
@generated function isnestedconcretetype(::Type{T}) where T
return isconcretetype(T) && all(isnestedconcretetype, T.parameters)
end

# match utils

literal_match_regex(s::Union{AbstractString, AbstractChar}, flags...) = Regex(Base.wrap_string(s, UInt32(0)), flags...)
Expand Down Expand Up @@ -739,8 +744,15 @@ end
## static multiple sample
(st::SequenceTemplate{T})(val::Val, xs::AbstractArray{<:AbstractVector{<:AbstractVector{T}}}) where T = map(apply_template(st, val), xs)

## dynamic
## deep nested or dynamic
function (st::SequenceTemplate{T})(val::Val, xs::AbstractArray) where T
if isnestedconcretetype(typeof(xs))
ET = Core.Compiler.return_type(st, Tuple{typeof(val), eltype(xs)})
RT = Array{ET, ndims(xs)}
y = RT(undef, size(xs))
map!(st(val), y, xs)
return y
end
aoa, aov = allany(Base.Fix2(isa, AbstractArray), xs)
if aoa
if all(Base.Fix1(all, Base.Fix2(isa, T)), xs) # dynamic single sample
Expand Down

0 comments on commit 92f77ed

Please sign in to comment.