From 33b906f9c094af9fac65eaacd30905efc65e3404 Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Sun, 29 Sep 2019 15:15:04 +0200 Subject: [PATCH] mod(n, range) (#661) * https://github.com/JuliaLang/julia/pull/32628 --- README.md | 2 ++ src/Compat.jl | 6 ++++++ test/runtests.jl | 20 +++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2751b9d14..90fcabec2 100644 --- a/README.md +++ b/README.md @@ -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]). diff --git a/src/Compat.jl b/src/Compat.jl index 1be0250ec..8fa251337 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index b6a873768..a4ab5412c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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)) @@ -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