Skip to content

Commit 29e95d7

Browse files
jiegilletmartinholters
authored andcommitted
Added eachrow/col/slice for v1.0 (#658)
* Added eachrow/col/slice for v1.0
1 parent a6b2831 commit 29e95d7

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Currently, the `@compat` macro supports the following syntaxes:
114114

115115
## New functions, macros, and methods
116116

117+
* `eachrow`, `eachcol`, and `eachslice` to iterate over first, second, or given dimension
118+
of an array ([#29749]).
119+
117120
* `isnothing` for testing if a variable is equal to `nothing` ([#29674]).
118121

119122
* `Compat.readline` with `keep` keyword argument ([#25646])
@@ -570,3 +573,4 @@ includes this fix. Find the minimum version from there.
570573
[#28850]: https://github.com/JuliaLang/julia/issues/28850
571574
[#29259]: https://github.com/JuliaLang/julia/issues/29259
572575
[#29674]: https://github.com/JuliaLang/julia/issues/29674
576+
[#29749]: https://github.com/JuliaLang/julia/issues/29749

src/Compat.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,21 @@ if VERSION < v"1.1.0-DEV.472"
931931
isnothing(::Nothing) = true
932932
end
933933

934+
# https://github.com/JuliaLang/julia/pull/29749
935+
@static if v"0.7" <= VERSION < v"1.1.0-DEV.792"
936+
export eachrow, eachcol, eachslice
937+
eachrow(A::AbstractVecOrMat) = (view(A, i, :) for i in axes(A, 1))
938+
eachcol(A::AbstractVecOrMat) = (view(A, :, i) for i in axes(A, 2))
939+
@inline function eachslice(A::AbstractArray; dims)
940+
length(dims) == 1 || throw(ArgumentError("only single dimensions are supported"))
941+
dim = first(dims)
942+
dim <= ndims(A) || throw(DimensionMismatch("A doesn't have $dim dimensions"))
943+
idx1, idx2 = ntuple(d->(:), dim-1), ntuple(d->(:), ndims(A)-dim)
944+
return (view(A, idx1..., i, idx2...) for i in axes(A, dim))
945+
end
946+
end
947+
948+
934949

935950
@static if !isdefined(Base, :Some)
936951
import Base: promote_rule, convert

test/runtests.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ end
3030
@test !isnothing(1)
3131
@test isnothing(nothing)
3232

33+
# https://github.com/JuliaLang/julia/pull/29749
34+
if VERSION >= v"0.7"
35+
@testset "row/column/slice iterators" begin
36+
# Simple ones
37+
M = [1 2 3; 4 5 6; 7 8 9]
38+
@test collect(eachrow(M)) == collect(eachslice(M, dims = 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
39+
@test collect(eachcol(M)) == collect(eachslice(M, dims = 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
40+
@test_throws DimensionMismatch eachslice(M, dims = 4)
41+
42+
# Higher-dimensional case
43+
M = reshape([(1:16)...], 2, 2, 2, 2)
44+
@test_throws MethodError collect(eachrow(M))
45+
@test_throws MethodError collect(eachcol(M))
46+
@test collect(eachslice(M, dims = 1))[1][:, :, 1] == [1 5; 3 7]
47+
end
48+
end
49+
3350
# julia#26365
3451
@test Compat.tr([1 2; 3 5]) == 6
3552

0 commit comments

Comments
 (0)