Skip to content

Commit a5df998

Browse files
fredrikekreararslan
authored andcommitted
support for firstindex (#494)
1 parent da17235 commit a5df998

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ Currently, the `@compat` macro supports the following syntaxes:
272272

273273
* `Dates.Period` rounding (e.g., `round(Dates.Hour(36), Dates.Day, RoundNearestTiesUp) == Dates.Day(2)` ([#24182]).
274274

275+
* `firstindex` to obtain the first index of an iterable ([#25458]).
276+
275277

276278
## Renaming
277279

src/Compat.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,14 @@ end
15521552
@static if !isdefined(Base, :lastindex)
15531553
const lastindex = endof
15541554
export lastindex
1555+
firstindex(a::AbstractArray) = (Base.@_inline_meta; first(linearindices(a)))
1556+
firstindex(c::Char) = 1
1557+
firstindex(c::Number) = 1
1558+
firstindex(p::Pair) = 1
1559+
firstindex(cmd::Cmd) = firstindex(cmd.exec)
1560+
firstindex(s::AbstractString) = 1
1561+
firstindex(t::Tuple) = 1
1562+
export firstindex
15551563
end
15561564

15571565
# 0.7.0-DEV.3585

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,7 @@ end
13301330
# 0.7.0-DEV.3583
13311331
@test lastindex(zeros(4)) == 4
13321332
@test lastindex(zeros(4,4)) == 16
1333+
@test all(x -> firstindex(x) == 1, ([1, 2], [1 2; 3 4], 'a', 1, 1=>2, `foo`, "foo", (1, 2)))
13331334

13341335
# 0.7.0-DEV.3585
13351336
let buf = IOBuffer()

0 commit comments

Comments
 (0)