Skip to content

Commit

Permalink
add partially-applied versions of ∋, ∉, ∌, occursin (#38475)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Dec 7, 2020
1 parent 7329e71 commit ef6ef82
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ New library features
* `keys(io::IO)` has been added, which returns all keys of `io` if `io` is an `IOContext` and an empty
`Base.KeySet` otherwise ([#37753]).
* `count` now accepts an optional `init` argument to control the accumulation type ([#37461]).
* New method `occursin(haystack)` that returns a function that checks whether its argument occurs in
`haystack` ([#38475]).
* New methods `∉(collection)`, `∋(item)`, and `∌(item)` returning corresponding containment-testing
functions ([#38475]).

Standard library changes
------------------------
Expand Down
32 changes: 25 additions & 7 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,14 @@ julia> map(Base.splat(+), zip(1:3,4:6))
"""
splat(f) = args->f(args...)

## in & contains
## in and related operators

"""
in(x)
in(collection)
∈(collection)
Create a function that checks whether its argument is [`in`](@ref) `x`, i.e.
a function equivalent to `y -> y in x`. See also [`insorted`](@ref) for the use
Create a function that checks whether its argument is [`in`](@ref) `collection`, i.e.
a function equivalent to `y -> y in collection`. See also [`insorted`](@ref) for use
with sorted collections.
The returned function is of type `Base.Fix2{typeof(in)}`, which can be
Expand All @@ -1120,14 +1121,31 @@ function in(x, itr)
end

const = in
(itr, x) = (x, itr)
(x, itr) = !(x, itr)
(itr) = Fix2(, itr)

"""
∋(collection, item) -> Bool
Like [`in`](@ref), but with arguments in reverse order.
Avoid adding methods to this function; define `in` instead.
"""
(itr, x) = in(x, itr)

"""
∋(item)
Create a function that checks whether its argument contains the given `item`, i.e.
a function equivalent to `y -> item in y`.
"""
(x) = Fix2(, x)

(itr, x) = !(itr, x)
(x) = Fix2(, x)

"""
in(item, collection) -> Bool
∈(item, collection) -> Bool
∋(collection, item) -> Bool
Determine whether an item is in the given collection, in the sense that it is
[`==`](@ref) to one of the values generated by iterating over the collection.
Expand Down Expand Up @@ -1194,7 +1212,7 @@ julia> [1, 2] .∈ ([2, 3],)
1
```
"""
in,
in

"""
∉(item, collection) -> Bool
Expand Down
10 changes: 10 additions & 0 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,4 +621,14 @@ See also: [`contains`](@ref).
occursin(needle::Union{AbstractString,AbstractChar}, haystack::AbstractString) =
_searchindex(haystack, needle, firstindex(haystack)) != 0

"""
occursin(haystack)
Create a function that checks whether its argument occurs in `haystack`, i.e.
a function equivalent to `needle -> occursin(needle, haystack)`.
The returned function is of type `Base.Fix2{typeof(occursin)}`.
"""
occursin(haystack) = Base.Fix2(occursin, haystack)

in(::AbstractString, ::AbstractString) = error("use occursin(x, y) for string containment")
9 changes: 9 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,14 @@ end
@test lt5(4) && !lt5(5)
end

@testset "ni" begin
@test ([1,5,10,11], 5)
@test !([1,10,11], 5)
@test (5)([5,1])
@test !(42)([0,1,100])
@test (0)(1:10)
@test (0)(-2:2)
end

a = rand(3, 3)
@test transpose(a) === a'
3 changes: 3 additions & 0 deletions test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ end
# occursin with a String and Char needle
@test occursin("o", "foo")
@test occursin('o', "foo")
# occursin in curried form
@test occursin("foo")("o")
@test occursin("foo")('o')

# contains
@test contains("foo", "o")
Expand Down

0 comments on commit ef6ef82

Please sign in to comment.