Skip to content

Define pairs(A) = enumerate(A) #34851

@jlapeyre

Description

@jlapeyre

The method for findall with no specialization on the collection type is

findall(testf::Function, A) = collect(first(p) for p in pairs(A) if testf(last(p)))

For a singly-linked list, like DataStructures.Cons, findall fails because there is no method keys(::Cons). Constructing, say linear indices for Cons requires traversing the list to find it's length; more precisely, I don't see a way around traversal. Writing a fallback method

pairs(A) = enumerate(A)

would solve the problem for any iterable, including Cons. This assumes that
by default the keys for a collection are 1, 2, ....
In the case of Cons, it's possible that traversing twice, preallocating output after the first pass, would be more efficient. I did not try this, but I doubt it is more efficient.

See JuliaCollections/DataStructures.jl#580 .

A somewhat related issue: I think the single argument method for findall, which is currently

findall(A) = collect(first(p) for p in pairs(A) if last(p))

would be better refactored as

findall(A) = findall(isequal(true), A)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions