# How much can hard-coded indexing be avoided in Julia?

**URL:** <https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699>\
**Category:** Internals & Design\
**Created:** [February 20, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699 "2021-02-20T20:23:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [February 20, 2021, 8:23pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/1 "2021-02-20T20:23:42Z")

</div>

I’ve noticed a trend toward Julia’s new keywords trending towards using keywords and functions instead of integer indexing in Julia. For example, first(array) instead of array[1], or eachindex(array) instead of 1:length(array).

Is this an intended aim for Julia style-- to avoid integer indexes in code that can avoid it? How likely is it that in the future Julia code will not need to show its 1-based integer heritage in common written code?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 20, 2021, 8:28pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/2 "2021-02-20T20:28:49Z")

</div>

I don’t understand what is being asked exactly.

> [@wherrera](#):
>
> Is this an intended aim for Julia style

Not relying on hard-coded indexing makes code more general (can handle non 1-based index array) while improving the readability, so, win-win? (third win: sometimes this helps compiler to optimize bound check away too)

> [@wherrera](#):
>
> will not need to show its 1-based integer heritage in common written code

not sure why this needs to be achieved. Again, people should strive to write generic code that works with arbitrarily index array (whenever possible) but there’s no universal effort to “hide” anything.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [February 20, 2021, 8:51pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/3 "2021-02-20T20:51:07Z")

</div>

> [@wherrera](#):
>
> eachindex(array)

This falls into a another, more important category as it ensures an ordering which is (the most) efficient for the passed in type, see [Arrays · The Julia Language](https://docs.julialang.org/en/v1/base/arrays/#Base.eachindex)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 20, 2021, 9:30pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/4 "2021-02-20T21:30:47Z")

</div>

You can also avoid indexing at all in many problems, just iterate the elements themselves. And why not!? Compare the following:

```julia
[f(x) for x in myvalues]

```

and

```julia
[f(myvalues[i]) for i in 1:length(myvalues)]

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 20, 2021, 9:38pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/5 "2021-02-20T21:38:38Z")

</div>

See [this answer of mine](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/27).

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [February 20, 2021, 11:57pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/6 "2021-02-20T23:57:16Z")

</div>

Henrique said in his answer pointed to above:

```
the decision ends up being mostly stylistic.

```

I asked because am wondering if the preferred style in Julia is going to move toward the newer usage?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 21, 2021, 12:04am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/7 "2021-02-21T00:04:21Z")

</div>

if you assume people prefer to write in win-win-win style instead of lose-lose-tie style, then yes.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [February 21, 2021, 12:50am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/8 "2021-02-21T00:50:48Z")

</div>

I did not say it in the exact post I linked above, but three posts below, and the the context is:

> [@Any shortcut to 1:length(myVector)?](https://discourse.julialang.org/t/any-shortcut-to-1-length-myvector/52816/30):
>
> if the code is guaranteed to not change too much (i.e., will keep using `Vector` s and not making so many accesses) then the decision ends up being mostly stylistic.

But yes, I would like to believe that given time (and that nothing changes in `Base`) more and more people will adopt one of that three alternatives (given their specific use). If you use only values, there really no reason to not go with just `for element in collection`; use only indexes? go with `for index in eachindex(collection)`; use both indexes and values? `for (index, element) in pairs(collection)`. There can be exceptions given some particular characteristics of your loop, but these should be considered the default in the mentioned circumnstances.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 21, 2021, 2:06am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/10 "2021-02-21T02:06:39Z")

</div>

`begin`

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [February 21, 2021, 4:32am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/11 "2021-02-21T04:32:05Z")

</div>

As a proponent of zero-based indexing, I strongly favor `first[array]` as a natural and intuitive way to express `array[0]` 😉

Don’t even mention the abomination that is `array[1]`. Hasn’t anyone read Dijskstra?

Edit: Irony, joke, absurdity!

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 21, 2021, 4:43am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/12 "2021-02-21T04:43:30Z")

</div>

The distinction between the serial number of an element of an array and the offset of the element is well understood. Sometimes one is of use, other times the other one is more helpful. Just because Dijskstra made some boldly worded statements doesn’t mean his viewpoint is the only one that is valid.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [February 21, 2021, 4:48am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/13 "2021-02-21T04:48:29Z")

</div>

Yeah I wasn’t serious. People often cite Dijkstra as an “objective” reason why slicing 0:n is superior, when it is objectively subjective.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [February 21, 2021, 4:59am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/14 "2021-02-21T04:59:25Z")

</div>

Perhaps the documentation needs to be more explicit about this. Personally I learn from examples, so that’d be a big plus for me.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [February 21, 2021, 7:14am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/15 "2021-02-21T07:14:59Z")

</div>

> [@apo383](#):
>
> Hasn’t anyone read Dijskstra?

Yes, and it’s completely subjective.

t doesn’t make sense to say 0-based indexing is superior to 1-based, or viceversa: they’re applicable in different contexts.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 21, 2021, 7:50pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/16 "2021-02-21T19:50:39Z")

</div>

Should we also mention `axes` here?

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [February 21, 2021, 8:06pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/17 "2021-02-21T20:06:17Z")

</div>

I wonder how index and value of the elements starting from the second can be obtained. There is an `Iterators.peel` which gives you first element and the an iterator over the rest, but how to get their indices as well?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 21, 2021, 8:11pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/18 "2021-02-21T20:11:41Z")

</div>

> [@jling](#):
>
> begin

To clarify, since Julia 1.4 you can use `begin` in indexing expressions to refer to the first index, e.g. `a[begin]` or `a[begin+1:end]` or (for multiple axes) `a[:, begin:2:end]`.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 21, 2021, 10:07pm UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/19 "2021-02-21T22:07:43Z")

</div>

While it’s implicit in some of the responses you’ve gotten here, it’s worth mentioning that unlike some other languages, Julia has good (but still not perfect) support for arrays with arbitrary indices: [Knowing where you are: custom array indices in Julia](https://julialang.org/blog/2017/04/offset-arrays/)

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [March 3, 2021, 3:51am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/20 "2021-03-03T03:51:11Z")

</div>

What’s the idiomatic (performant) way of manipulating indices?  
For example, suppose that I want to fill an `Array` `A` with the values of the function `f`, which depend on the indices, but require some manipulation, with another function `g`:

```julia
A = Array{Float64}(undef, 10, 20, 30)
for i in 1:10, j in 1:20, k in 1:30
    A[i, j, k] = f(i, g(j, k))
end

```

I encounter situations like this one very often.

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 3, 2021, 3:54am UTC](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699/21 "2021-03-03T03:54:25Z")

</div>

Except for the order of the indices, I don’t see what else you should change. You could precompute the outer loop value `g(j, k)`.

[Next page](https://discourse.julialang.org/t/how-much-can-hard-coded-indexing-be-avoided-in-julia/55699.md?page=2)
