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

filter(::Function, ::Tuple) is not defined #30418

Closed
cstjean opened this issue Dec 17, 2018 · 2 comments · Fixed by #32968
Closed

filter(::Function, ::Tuple) is not defined #30418

cstjean opened this issue Dec 17, 2018 · 2 comments · Fixed by #32968

Comments

@cstjean
Copy link
Contributor

cstjean commented Dec 17, 2018

It should be defined? It would be consistent with the other methods:

julia> map(x->x+2, (1,2,3))
(3, 4, 5)

julia> filter(iseven, (1,2,3))
ERROR: MethodError: no method matching filter(::typeof(iseven), ::Tuple{Int64,Int64,Int64})
Closest candidates are:
  filter(::Any, ::Array{T,1} where T) at array.jl:2352
  filter(::Any, ::BitArray) at bitarray.jl:1638
  filter(::Any, ::AbstractArray) at array.jl:2313
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> collect(Iterators.filter(iseven, (1,2,3)))
1-element Array{Int64,1}:
 2

I get that it's not a type-stable operation except in special cases like filter(isa(Int), (2,3.0,4)), but it would still be occasionally convenient.

@o314
Copy link
Contributor

o314 commented Jan 28, 2019

Idem with

filter(BitArray, itr)
filter(Vector{Bool}, itr)

patching

struct Filter{F,I}
    flt::F
    itr::I
end

with

struct Filter{F<:Function,I}
    flt::F
    itr::I
end

can do the trick for my 2 cts.

The problem may be restated: shall the base do it -or- shall it let us do it when needed.

If the compiler handles this issue by catching all the call eg. filter(s::Any, itr::Any) is for me, this will lead to both bottleneck and opaque interface/interop at module boundary.

@o314
Copy link
Contributor

o314 commented Jan 28, 2019

Have you notice that

@test tuple(Iterators.filter(isodd, (1, 2, 3, 4, 5))...) == (1,3,5)
Test Passed

works ?

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

Successfully merging a pull request may close this issue.

2 participants