diff --git a/base/range.jl b/base/range.jl index 4ddecd7bdd91c1..5233bd3ef95775 100644 --- a/base/range.jl +++ b/base/range.jl @@ -1151,6 +1151,13 @@ function intersect(r1::AbstractRange, r2::AbstractRange, r3::AbstractRange, r::A i end +function intersect(r::AbstractRange, vec::AbstractVector) + common = Iterators.filter(x -> x ∈ r, vec) + seen = Set{eltype(vec)}(common) + return vectorfilter(_shrink_filter!(seen), common) +end +intersect(vec::AbstractVector, r::AbstractRange) = intersect(r, vec) + # _findin (the index of intersection) function _findin(r::AbstractRange{<:Integer}, span::AbstractUnitRange{<:Integer}) local ifirst diff --git a/test/ranges.jl b/test/ranges.jl index 1c31585ece45e0..cb981bb1a2d961 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -403,6 +403,9 @@ end @test intersect(1:3, 2) === intersect(2, 1:3) === 2:2 @test intersect(1.0:3.0, 2) == intersect(2, 1.0:3.0) == [2.0] + @test intersect(1:typemax(Int), [1, 3]) === [1, 3] + @test intersect([1, 3], 1:typemax(Int)) === [1, 3] + @testset "Support StepRange with a non-numeric step" begin start = Date(1914, 7, 28) stop = Date(1918, 11, 11)