Skip to content

Commit

Permalink
Add filter(func) method (JuliaLang#41173)
Browse files Browse the repository at this point in the history
Add `filter(func)` method
  • Loading branch information
GlenHertz authored Sep 29, 2022
1 parent f346735 commit 6e1e6fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2633,6 +2633,33 @@ function filter!(f, a::AbstractVector)
return a
end

"""
filter(f)
Create a function that filters its arguments with function `f` using [`filter`](@ref), i.e.
a function equivalent to `x -> filter(f, x)`.
The returned function is of type `Base.Fix1{typeof(filter)}`, which can be
used to implement specialized methods.
# Examples
```jldoctest
julia> (1, 2, Inf, 4, NaN, 6) |> filter(isfinite)
(1, 2, 4, 6)
julia> map(filter(iseven), [1:3, 2:4, 3:5])
3-element Vector{Vector{Int64}}:
[2]
[2, 4]
[4]
```
!!! compat "Julia 1.8"
This method requires at least Julia 1.8.
"""
function filter(f)
Fix1(filter, f)
end

"""
keepat!(a::Vector, inds)
keepat!(a::BitVector, inds)
Expand Down
3 changes: 3 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,9 @@ end
@test isempty(eoa)
end

@testset "filter curried #41173" begin
@test -5:5 |> filter(iseven) == -4:2:4
end
@testset "logical keepat!" begin
# Vector
a = Vector(1:10)
Expand Down

0 comments on commit 6e1e6fa

Please sign in to comment.