Skip to content

random_namedtuple #34

Open
Open
@cscherrer

Description

@cscherrer

Hi @JeffreySarnoff , I'd like to contribute some of the things I've been working on lately. First up:

"""
    random_namedtuple(width, depth; names='a':'z')

Generate a random nested named tuple. This is useful for tests.

Example:

    julia> randnt(3,3)
    (e = (o = (l = :l, e = :e, s = :s), d = (x = :x, e = :e, a = :a), a = (i = :i, u = :u, d = :d)), 
    f = (b = (y = :y, k = :k, o = :o), a = (b = :b, f = :f, k = :k), z = (j = :j, b = :b, u = :u)), 
    t = (b = (q = :q, k = :k, s = :s), c = (d = :d, u = :u, a = :a), h = (u = :u, c = :c, m = :m)))
"""
function random_namedtuple(width, depth; names='a':'z')
    k = unique(Symbol.(rand(names, width)))
    if depth  1
        return namedtuple(k)(k)
    else
        nts = Tuple((randnt(width, depth-1; names=names) for _ in 1:length(k)))
        return namedtuple(k)(nts)
    end
end 

Any thoughts on this? If you like it, where should it go?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions