Skip to content

Commit 33b906f

Browse files
mcabbottmartinholters
authored andcommitted
mod(n, range) (#661)
* JuliaLang/julia#32628
1 parent 29e95d7 commit 33b906f

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Currently, the `@compat` macro supports the following syntaxes:
114114

115115
## New functions, macros, and methods
116116

117+
* `mod` now accepts a unit range as the second argument ([#32628]).
118+
117119
* `eachrow`, `eachcol`, and `eachslice` to iterate over first, second, or given dimension
118120
of an array ([#29749]).
119121

src/Compat.jl

+6
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,12 @@ if v"0.7.0" <= VERSION < v"1.1.0-DEV.594"
18661866
Base.merge(a::NamedTuple) = a
18671867
end
18681868

1869+
# https://github.com/JuliaLang/julia/pull/32628
1870+
if v"0.7.0" <= VERSION < v"1.3.0-alpha.8"
1871+
Base.mod(i::Integer, r::Base.OneTo) = mod1(i, last(r))
1872+
Base.mod(i::Integer, r::AbstractUnitRange{<:Integer}) = mod(i-first(r), length(r)) + first(r)
1873+
end
1874+
18691875
include("deprecated.jl")
18701876

18711877
end # module Compat

test/runtests.jl

+19-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if VERSION >= v"0.7"
3838
@test collect(eachrow(M)) == collect(eachslice(M, dims = 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
3939
@test collect(eachcol(M)) == collect(eachslice(M, dims = 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
4040
@test_throws DimensionMismatch eachslice(M, dims = 4)
41-
41+
4242
# Higher-dimensional case
4343
M = reshape([(1:16)...], 2, 2, 2, 2)
4444
@test_throws MethodError collect(eachrow(M))
@@ -1499,4 +1499,22 @@ end
14991499
@test merge((a=1,), (b=2,), (c=3,)) == (a=1,b=2,c=3)
15001500
end
15011501

1502+
# https://github.com/JuliaLang/julia/pull/32628
1503+
if VERSION >= v"0.7"
1504+
@testset "mod with ranges" begin
1505+
for n in -10:10
1506+
@test mod(n, 0:4) == mod(n, 5)
1507+
@test mod(n, 1:5) == mod1(n, 5)
1508+
@test mod(n, 2:6) == 2 + mod(n-2, 5)
1509+
@test mod(n, Base.OneTo(5)) == mod1(n, 5)
1510+
end
1511+
@test mod(Int32(3), 1:5) == 3
1512+
@test mod(big(typemax(Int))+99, 0:4) == mod(big(typemax(Int))+99, 5)
1513+
@test_throws MethodError mod(3.141, 1:5)
1514+
@test_throws MethodError mod(3, UnitRange(1.0,5.0))
1515+
@test_throws MethodError mod(3, 1:2:7)
1516+
@test_throws DivideError mod(3, 1:0)
1517+
end
1518+
end
1519+
15021520
nothing

0 commit comments

Comments
 (0)