Julia: beginof(a) analogue to endof(a)?

Created on 19 Aug 2017  路  6Comments  路  Source: JuliaLang/julia

Looping over 1:length(a) is wrong for AbstractVector because it fails for non 1-based arrays. eachindex(a) is correct, but occasionally you need to provide explicit indices, e.g. to loop only over odd indices as in #23267. start(a):endof(a) doesn't work because for views start(a) is a tuple. You can do first(eachindex(a)), but this is kind of klunky and non-obvious.

Can we export a function startof beginof(a) = first(eachindex(a)) analogous to endof(a)?

arrays

Most helpful comment

Or beginindex and endindex

All 6 comments

Since the consensus in #15750 was for a begin keyword for getindex expressions, similar to end, then the corresponding function should be beginof.

startof reads nicer than beginof, if this is open for naming comments...

@ararslan, startof was my initial thought as well, but if we implement a[begin:end] syntactic sugar (as seems to be the clear consensus), then I feel like it pretty much has to translate to beginof in analogy to endof.

We can implement the function before we implement any syntactic sugar for it, but we should still name it consistently with the planned sugar.

What about changing both to begin_index and end_index?

Or beginindex and endindex

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixrehren picture felixrehren  路  3Comments

sbromberger picture sbromberger  路  3Comments

omus picture omus  路  3Comments

ararslan picture ararslan  路  3Comments

dpsanders picture dpsanders  路  3Comments