Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema #35

Open
cscherrer opened this issue Dec 21, 2020 · 0 comments
Open

schema #35

cscherrer opened this issue Dec 21, 2020 · 0 comments

Comments

@cscherrer
Copy link
Contributor

I use this a lot for building generated functions. It turns the type of a named tuple into a named tuple. So you can manipulate it very easily and build static code specialized for a given type.

export schema

"""
    schema(::Type)
    
`schema` turns a type into a value that's easier to work with.

Example:

    julia> nt = (a=(b=[1,2],c=(d=[3,4],e=[5,6])),f=[7,8]);

    julia> NT = typeof(nt)
    NamedTuple{(:a, :f),Tuple{NamedTuple{(:b, :c),Tuple{Array{Int64,1},NamedTuple{(:d, :e),Tuple{Array{Int64,1},Array{Int64,1}}}}},Array{Int64,1}}}

    julia> schema(NT)
    (a = (b = Array{Int64,1}, c = (d = Array{Int64,1}, e = Array{Int64,1})), f = Array{Int64,1})
"""
function schema end

function schema(NT::Type{NamedTuple{names, T}}) where {names, T}
    return namedtuple(fieldnames(NT), schema(valtype(NT)))
end

function schema(TT::Type{T}) where {T <: Tuple} 
    return schema.(Tuple(TT.types))
end

schema(t::T) where {T <: Tuple} = schema(T) 

schema(t::T) where {T <: NamedTuple} = schema(T) 

schema(T) = T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant