-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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 is not documented #16742
Comments
A comment on your note, according to the source, this is not true. I am new to julia, so this is just a thought, but this is inconsistent and should be at least be better documented. |
I also noticed this inconsistency. Why not have Since Julia aims to be a language for HPC scientific computing I think it is important to give client programmers control over memory complexity. Returning an |
julia> methods(filter)
# 7 methods for generic function "filter":
filter(f, a::Array{T<:Any,1}) at array.jl:952
filter(f, Bs::BitArray) at bitarray.jl:1746
filter(f, As::AbstractArray) at array.jl:937
filter(f, d::Associative) at dict.jl:273
filter(f, s::Set) at set.jl:166
filter(f, s::AbstractString) at strings/basic.jl:279
filter(flt, itr) at iterator.jl:112 julia> filter(iseven,1:4) #The datatype of 1:4 is a subtype of AbstractArray, so filter return array
2-element Array{Int64,1}:
2
4
julia> filter(iseven,(i for i in 1:4)) #input is an iterator, so filter return iterator
Filter{Base.#iseven,Base.Generator{UnitRange{Int64},##7#8}}(iseven,Base.Generator{UnitRange{Int64},##7#8}(#7,1:4))
julia> filter(iseven,[i for i in 1:4]) #input is an array, so filter return array
2-element Array{Int64,1}:
2
4
julia> typeof(1:4)
UnitRange{Int64}
julia> UnitRange{Int64} <: AbstractArray
true
In my opinion, small or mediate sized arrays are more frequent used than large arrays among Julia users. If you want control memory, iterator can be used. I am also new to Julia, just try to offer what I know. Hope it will help. |
We discussed I would suggest that |
If you plan to iterate over a filtered collection more than once and do not want to apply the filter multiple times, you should collect the outcome of filter. This is explicit in the code and indicates that the author is more worried about CPU cycles than memory. I strongly believe that in computational science the CPU and memory complexity should be part of the API, meaning you should be able to read it from the code. I agree that there might be cases where a single function doing the filtering and collecting is more efficient than calling collect(filter(...)). Reading the Julia style docs a function called collect_filter is suggested... |
|
After some experimentation, I found that
Base.Filter
creates an iterator from a predicate and another iterator. Example:Filter(iseven, 1:7)
creates an iterator over the integers 2, 4, 6.
I couldn't find this in the documentation. But I think it's very convenient.
Note that this is distinct from the function
filter
, which instantiates a filtered collection, instead of an iterator.The text was updated successfully, but these errors were encountered: