Skip to content

Commit

Permalink
mod(n, range) (#661)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored and martinholters committed Sep 29, 2019
1 parent 29e95d7 commit 33b906f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Currently, the `@compat` macro supports the following syntaxes:

## New functions, macros, and methods

* `mod` now accepts a unit range as the second argument ([#32628]).

* `eachrow`, `eachcol`, and `eachslice` to iterate over first, second, or given dimension
of an array ([#29749]).

Expand Down
6 changes: 6 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,12 @@ if v"0.7.0" <= VERSION < v"1.1.0-DEV.594"
Base.merge(a::NamedTuple) = a
end

# https://github.com/JuliaLang/julia/pull/32628
if v"0.7.0" <= VERSION < v"1.3.0-alpha.8"
Base.mod(i::Integer, r::Base.OneTo) = mod1(i, last(r))
Base.mod(i::Integer, r::AbstractUnitRange{<:Integer}) = mod(i-first(r), length(r)) + first(r)
end

include("deprecated.jl")

end # module Compat
20 changes: 19 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if VERSION >= v"0.7"
@test collect(eachrow(M)) == collect(eachslice(M, dims = 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
@test collect(eachcol(M)) == collect(eachslice(M, dims = 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
@test_throws DimensionMismatch eachslice(M, dims = 4)

# Higher-dimensional case
M = reshape([(1:16)...], 2, 2, 2, 2)
@test_throws MethodError collect(eachrow(M))
Expand Down Expand Up @@ -1499,4 +1499,22 @@ end
@test merge((a=1,), (b=2,), (c=3,)) == (a=1,b=2,c=3)
end

# https://github.com/JuliaLang/julia/pull/32628
if VERSION >= v"0.7"
@testset "mod with ranges" begin
for n in -10:10
@test mod(n, 0:4) == mod(n, 5)
@test mod(n, 1:5) == mod1(n, 5)
@test mod(n, 2:6) == 2 + mod(n-2, 5)
@test mod(n, Base.OneTo(5)) == mod1(n, 5)
end
@test mod(Int32(3), 1:5) == 3
@test mod(big(typemax(Int))+99, 0:4) == mod(big(typemax(Int))+99, 5)
@test_throws MethodError mod(3.141, 1:5)
@test_throws MethodError mod(3, UnitRange(1.0,5.0))
@test_throws MethodError mod(3, 1:2:7)
@test_throws DivideError mod(3, 1:0)
end
end

nothing

0 comments on commit 33b906f

Please sign in to comment.