Julia: `findall` does not work with `Set`

Created on 18 Sep 2020  路  3Comments  路  Source: JuliaLang/julia

MWE:

julia> s = Set([1, 4, 5]);

julia> findall(x -> x > 3, s)
ERROR: MethodError: no method matching keys(::Set{Int64})
Closest candidates are:
  keys(::Cmd) at process.jl:639
  keys(::Core.SimpleVector) at essentials.jl:603
  keys(::Tuple) at tuple.jl:64
  ...
Stacktrace:
 [1] pairs(::Set{Int64}) at ./abstractdict.jl:134
 [2] findall(::var"#29#30", ::Set{Int64}) at ./array.jl:2108
 [3] top-level scope at REPL[29]:1

Most helpful comment

Why should findall work on Sets? findall returns the indices of the elements for which the predicate returns true, but a Set does not have indices.

All 3 comments

Defining

Base.keys(s::Set) = Base.keys(s.dict)

solves this.

Why should findall work on Sets? findall returns the indices of the elements for which the predicate returns true, but a Set does not have indices.

You're correct. I was looking for the function that returned the elements satisfying the predicate. Sorry for the noise.

EDIT: That function is filter.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TotalVerb picture TotalVerb  路  3Comments

musm picture musm  路  3Comments

manor picture manor  路  3Comments

iamed2 picture iamed2  路  3Comments

Keno picture Keno  路  3Comments