From 2b34fbd3f716599f1a6ca4af2c7f9e9b5c669167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Gillet?= Date: Wed, 24 Jul 2019 21:56:44 +0900 Subject: [PATCH 1/8] Added eachrow/col/slice for v1.0 --- README.md | 7 +++++++ src/Compat.jl | 15 +++++++++++++++ test/runtests.jl | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/README.md b/README.md index 871d85d4a..9bbe6ebeb 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,12 @@ Currently, the `@compat` macro supports the following syntaxes: ## New functions, macros, and methods +* `eachslice` to iterate over some dimension of a matrix ([#29749]). + +* `eachcol` to iterate over the second dimension of a matrix ([#29749]). + +* `eachrow` to iterate over the first dimension of a matrix ([#29749]). + * `isnothing` for testing if a variable is equal to `nothing` ([#29674]). * `Compat.readline` with `keep` keyword argument ([#25646]) @@ -570,3 +576,4 @@ includes this fix. Find the minimum version from there. [#28850]: https://github.com/JuliaLang/julia/issues/28850 [#29259]: https://github.com/JuliaLang/julia/issues/29259 [#29674]: https://github.com/JuliaLang/julia/issues/29674 +[#29749]: https://github.com/JuliaLang/julia/issues/29749 diff --git a/src/Compat.jl b/src/Compat.jl index a4533bba4..27b583a4f 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -931,6 +931,21 @@ if VERSION < v"1.1.0-DEV.472" isnothing(::Nothing) = true end +# https://github.com/JuliaLang/julia/pull/29749 +if VERSION < v"1.1.0" + export eachrow, eachcol, eachslice + eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) + eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) + @inline function eachslice(A::AbstractArray; dims) + length(dims) == 1 || throw(ArgumentError("only single dimensions are supported")) + dim = first(dims) + dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions")) + idx1, idx2 = ntuple(d->(:), dim-1), ntuple(d->(:), ndims(A)-dim) + return (view(A, idx1..., i, idx2...) for i in axes(A, dim)) + end +end + + @static if !isdefined(Base, :Some) import Base: promote_rule, convert diff --git a/test/runtests.jl b/test/runtests.jl index 9bea19403..263640aa1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -30,6 +30,21 @@ end @test !isnothing(1) @test isnothing(nothing) +# https://github.com/JuliaLang/julia/pull/29749 +@testset "row/column/slice iterators" begin + # Simple ones + M = [1 2 3; 4 5 6; 7 8 9] + @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)) + @test_throws MethodError collect(eachcol(M)) + @test collect(eachslice(M, dims = 1))[1][:, :, 1] == [1 5; 3 7] +end + # julia#26365 @test Compat.tr([1 2; 3 5]) == 6 From 338ba597bec2a0cebbdfcc9024717c6bb62a47dc Mon Sep 17 00:00:00 2001 From: Jie Date: Tue, 20 Aug 2019 09:28:15 +0900 Subject: [PATCH 2/8] More precise version and indentiation fix --- README.md | 7 +------ src/Compat.jl | 16 ++++++++-------- test/runtests.jl | 26 ++++++++++++++------------ 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 9bbe6ebeb..dcd2897c2 100644 --- a/README.md +++ b/README.md @@ -114,12 +114,7 @@ Currently, the `@compat` macro supports the following syntaxes: ## New functions, macros, and methods -* `eachslice` to iterate over some dimension of a matrix ([#29749]). - -* `eachcol` to iterate over the second dimension of a matrix ([#29749]). - -* `eachrow` to iterate over the first dimension of a matrix ([#29749]). - +* `eachcol` to iterate over the second dimension of a matrix and `eachrow` to iterate over the first dimension of a matrix and `eachslice` to iterate over some dimension of a matrix ([#29749]). * `isnothing` for testing if a variable is equal to `nothing` ([#29674]). * `Compat.readline` with `keep` keyword argument ([#25646]) diff --git a/src/Compat.jl b/src/Compat.jl index 27b583a4f..8070c6a94 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -932,17 +932,17 @@ if VERSION < v"1.1.0-DEV.472" end # https://github.com/JuliaLang/julia/pull/29749 -if VERSION < v"1.1.0" +if v"7.0" <= VERSION < v"1.1.0-DEV.792" export eachrow, eachcol, eachslice - eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) + eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) @inline function eachslice(A::AbstractArray; dims) - length(dims) == 1 || throw(ArgumentError("only single dimensions are supported")) - dim = first(dims) - dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions")) - idx1, idx2 = ntuple(d->(:), dim-1), ntuple(d->(:), ndims(A)-dim) - return (view(A, idx1..., i, idx2...) for i in axes(A, dim)) - end + length(dims) == 1 || throw(ArgumentError("only single dimensions are supported")) + dim = first(dims) + dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions")) + idx1, idx2 = ntuple(d->(:), dim-1), ntuple(d->(:), ndims(A)-dim) + return (view(A, idx1..., i, idx2...) for i in axes(A, dim)) + end end diff --git a/test/runtests.jl b/test/runtests.jl index 263640aa1..ce0bd816a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,18 +31,20 @@ end @test isnothing(nothing) # https://github.com/JuliaLang/julia/pull/29749 -@testset "row/column/slice iterators" begin - # Simple ones - M = [1 2 3; 4 5 6; 7 8 9] - @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)) - @test_throws MethodError collect(eachcol(M)) - @test collect(eachslice(M, dims = 1))[1][:, :, 1] == [1 5; 3 7] +if VERSION > v"0.7" + @testset "row/column/slice iterators" begin + # Simple ones + M = [1 2 3; 4 5 6; 7 8 9] + @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)) + @test_throws MethodError collect(eachcol(M)) + @test collect(eachslice(M, dims = 1))[1][:, :, 1] == [1 5; 3 7] + end end # julia#26365 From fa0851ac90701650483be00b77dba334b41b34ff Mon Sep 17 00:00:00 2001 From: Jie Date: Tue, 20 Aug 2019 09:29:42 +0900 Subject: [PATCH 3/8] line spacing --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dcd2897c2..ce84c0faf 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ Currently, the `@compat` macro supports the following syntaxes: ## New functions, macros, and methods * `eachcol` to iterate over the second dimension of a matrix and `eachrow` to iterate over the first dimension of a matrix and `eachslice` to iterate over some dimension of a matrix ([#29749]). + * `isnothing` for testing if a variable is equal to `nothing` ([#29674]). * `Compat.readline` with `keep` keyword argument ([#25646]) From f6df7c069d8810915947062a8a2d67a221b224e5 Mon Sep 17 00:00:00 2001 From: Jie Date: Tue, 20 Aug 2019 09:46:27 +0900 Subject: [PATCH 4/8] No such thing as v7.0 yet --- src/Compat.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compat.jl b/src/Compat.jl index 8070c6a94..9c880725a 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -932,7 +932,7 @@ if VERSION < v"1.1.0-DEV.472" end # https://github.com/JuliaLang/julia/pull/29749 -if v"7.0" <= VERSION < v"1.1.0-DEV.792" +if v"0.7" <= VERSION < v"1.1.0-DEV.792" export eachrow, eachcol, eachslice eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) From db56e71f58c87fea9857983f52c7efa3da8b3f70 Mon Sep 17 00:00:00 2001 From: Jie Date: Tue, 20 Aug 2019 10:09:28 +0900 Subject: [PATCH 5/8] Adding default value of dim for 0.6 syntax compatibility --- src/Compat.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compat.jl b/src/Compat.jl index 9c880725a..e1eb748bc 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -936,7 +936,7 @@ if v"0.7" <= VERSION < v"1.1.0-DEV.792" export eachrow, eachcol, eachslice eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) - @inline function eachslice(A::AbstractArray; dims) + @inline function eachslice(A::AbstractArray; dims = 1) length(dims) == 1 || throw(ArgumentError("only single dimensions are supported")) dim = first(dims) dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions")) From 8605ece638dad47cf13073633c0d17b39e595396 Mon Sep 17 00:00:00 2001 From: Jie Date: Wed, 21 Aug 2019 09:40:40 +0900 Subject: [PATCH 6/8] Used @static to bypass 0.6 syntax conflict --- src/Compat.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Compat.jl b/src/Compat.jl index e1eb748bc..1be0250ec 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -932,11 +932,11 @@ if VERSION < v"1.1.0-DEV.472" end # https://github.com/JuliaLang/julia/pull/29749 -if v"0.7" <= VERSION < v"1.1.0-DEV.792" +@static if v"0.7" <= VERSION < v"1.1.0-DEV.792" export eachrow, eachcol, eachslice eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1)) eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2)) - @inline function eachslice(A::AbstractArray; dims = 1) + @inline function eachslice(A::AbstractArray; dims) length(dims) == 1 || throw(ArgumentError("only single dimensions are supported")) dim = first(dims) dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions")) From 4b4b4643f7751a2f30874b8826810812d5143723 Mon Sep 17 00:00:00 2001 From: Jie Date: Wed, 21 Aug 2019 21:35:24 +0900 Subject: [PATCH 7/8] Update README.md Co-Authored-By: Martin Holters --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce84c0faf..2751b9d14 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,8 @@ Currently, the `@compat` macro supports the following syntaxes: ## New functions, macros, and methods -* `eachcol` to iterate over the second dimension of a matrix and `eachrow` to iterate over the first dimension of a matrix and `eachslice` to iterate over some dimension of a matrix ([#29749]). +* `eachrow`, `eachcol`, and `eachslice` to iterate over first, second, or given dimension + of an array ([#29749]). * `isnothing` for testing if a variable is equal to `nothing` ([#29674]). From 67d2b2ab0b161e0bc13e10d6d6013a9fff2e4462 Mon Sep 17 00:00:00 2001 From: Jie Date: Wed, 21 Aug 2019 21:35:36 +0900 Subject: [PATCH 8/8] Update test/runtests.jl Co-Authored-By: Martin Holters --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index ce0bd816a..b6a873768 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,7 +31,7 @@ end @test isnothing(nothing) # https://github.com/JuliaLang/julia/pull/29749 -if VERSION > v"0.7" +if VERSION >= v"0.7" @testset "row/column/slice iterators" begin # Simple ones M = [1 2 3; 4 5 6; 7 8 9]