Getting copy of filtered collection in Julia – filter() and filter!() Methods
The filter() is an inbuilt function in julia which is used to return a copy of the specified abstract array with different types of filtration performed. These all operation are illustrated with below examples.
Syntax:
filter(f, a::AbstractArray)
or
filter(f, d::AbstractDict)
or
filter(f, itr::SkipMissing{<:AbstractArray})
Parameters:
- f: Specified instruction.
- a::AbstractArray: Specified array.
- d::AbstractDict: Specified dictionary.
- itr::SkipMissing: Specified SkipMissing iterator.
Returns: It returns a copy of the specified abstract array with different types of filter operation.
Example 1: This example returns a copy of a, removing elements for which f is false.
Python
a = 5 : 20
println( filter (isodd, a))
println( filter (iseven, a))
|
Output:
[5, 7, 9, 11, 13, 15, 17, 19]
[6, 8, 10, 12, 14, 16, 18, 20]
Example 2: This example returns a copy of the specified dictionary, removing elements for which f is false.
Python
A = Dict ( 1 = >"a", 2 = >"b", 3 = >"c")
println( filter (p - >isodd(p.first), A))
println( filter (p - >iseven(p.first), A))
|
Output:
Example 3: This example returns a vector similar to the array wrapped by the given SkipMissing iterator but with all missing elements and those for which f returns false removed.
Python
A = [ 5 7 ; missing 8 ; 10 11 ]
println( filter (isodd, skipmissing(A)))
println( filter (iseven, skipmissing(A)))
|
Output:
filter!()
The filter!() is an inbuilt function in julia which is used to update the specified abstract array with different types of filtration performed. These all operation are illustrated with below examples.
Syntax:
filter!(f, a::AbstractArray)
or
filter!(f, d::AbstractDict)
Parameters:
- f: Specified instruction.
- a::AbstractArray: Specified array.
- d::AbstractDict: Specified dictionary.
Returns: It returns the updated specified abstract array with different types of filter operation.
Example 1: This example returns updated a, removing elements for which f is false.
Python
a = Vector( 2 : 10 )
println( filter !(isodd, a))
b = Vector( 2 : 10 )
println( filter !(iseven, b))
|
Output:
[3, 5, 7, 9]
[2, 4, 6, 8, 10]
Example 2: This example returns the updated specified dictionary, removing elements for which f is false.
Python
A = Dict ( 1 = >"a", 2 = >"b", 3 = >"c")
println( filter !(p - >isodd(p.first), A))
B = Dict ( 1 = >"a", 2 = >"b", 3 = >"c")
println( filter !(p - >iseven(p.first), B))
|
Output:
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...