Skip to content

Static findfirst, findall for Tuple #13

@mtfishman

Description

@mtfishman

Here is a use case that I think this package could help with. I have a situation where I want to split a tuple like this:

t = (1, "X", 1, 2, "Y", 2, "Z", 4)

into a series of tuples:

t1, t2, t3, t4 = ((1,), ("X", 1, 2), ("Y", 2), ("Z", 4))

where the start of each split tuple is the location of an element of a certain type (in this case, AbstractString).

Here is a very type unstable version:

julia> t = (1, "X", 1, 2, "Y", 2, "Z", 4)
(1, "X", 1, 2, "Y", 2, "Z", 4)

julia> function split(f, t::Tuple)
         n = findall(f, t)
         ti = t[1:(first(n) - 1)]
         ts = ntuple(i -> t[n[i]:(n[i + 1] - 1)], length(n) - 1)
         tf = t[last(n):end]
         return ti, ts..., tf
       end
split (generic function with 1 method)

julia> split(x -> x isa AbstractString, t)
((1,), ("X", 1, 2), ("Y", 2), ("Z", 4))

It seems like this is a case that could benefit from static numbers, ranges, and slices (like ones discussed in #4). Can something like this be made type stable with this package? It seems like it would require a version of findall for Tuples that output static index locations.

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