Skip to content

Commit 8257b8b

Browse files
authored
Add eachindex function (#151)
1 parent aefc1fe commit 8257b8b

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/tableselection.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Tables.rows(t::TableSelection) = SelectionRows(t)
4242
Tables.columnnames(t::TableSelection) = t.names
4343

4444
function Tables.getcolumn(t::TableSelection, i::Int)
45-
i > t.ncols && error("Table has no column with index $i.")
45+
1 i t.ncols || error("Table has no column with index $i.")
4646
Tables.getcolumn(t.cols, t.mapnames[t.names[i]])
4747
end
4848

@@ -100,6 +100,7 @@ Base.IteratorEltype(::Type{<:SelectionRow}) = Base.EltypeUnknown()
100100
# Indexing interface
101101
Base.firstindex(::SelectionRow) = 1
102102
Base.lastindex(row::SelectionRow) = row.ncols
103+
Base.eachindex(row::SelectionRow) = 1:row.ncols
103104
Base.getindex(row::SelectionRow, i::Int) = Tables.getcolumn(row, i)
104105

105106
# Tables.jl row interface
@@ -134,14 +135,15 @@ end
134135
Base.iterate(s::SelectionRows, state::Int=1) =
135136
state > s.nrows ? nothing : (s[state], state + 1)
136137

137-
Base.length(row::SelectionRows) = row.nrows
138+
Base.length(s::SelectionRows) = s.nrows
138139
Base.eltype(::Type{SelectionRows{T}}) where {T} = SelectionRow{T}
139140
Base.IteratorSize(::Type{<:SelectionRows}) = Base.HasLength()
140141
Base.IteratorEltype(::Type{<:SelectionRows}) = Base.HasEltype()
141142

142143
# Indexing interface
143144
Base.firstindex(::SelectionRows) = 1
144145
Base.lastindex(s::SelectionRows) = s.nrows
146+
Base.eachindex(s::SelectionRows) = 1:s.nrows
145147
Base.getindex(s::SelectionRows, i::Int) = SelectionRow(s.selection, i)
146148

147149
# Tables.jl interface

test/tableselection.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
s = TableTransforms.TableSelection(t, [:x, :y, :z], [:c, :d, :f])
6060
@test_throws ErrorException Tables.getcolumn(s, :c)
6161
@test_throws ErrorException Tables.getcolumn(s, 4)
62+
@test_throws ErrorException Tables.getcolumn(s, -2)
6263
end
6364

6465
@testset "SelectionRow" begin
@@ -95,7 +96,7 @@
9596
srow = TableTransforms.SelectionRow(s, i)
9697
@test firstindex(srow) == 1
9798
@test lastindex(srow) == lastindex(row)
98-
for j in eachindex(row)
99+
for j in eachindex(srow)
99100
@test srow[j] == row[j]
100101
end
101102
end
@@ -139,7 +140,7 @@
139140
# Indexing interface
140141
@test firstindex(srows) == 1
141142
@test lastindex(srows) == TableTransforms._nrows(s.cols)
142-
for i in 1:length(srows)
143+
for i in eachindex(srows)
143144
@test srows[i] == TableTransforms.SelectionRow(s, i)
144145
end
145146
end

0 commit comments

Comments
 (0)