# Easiest way to make tenth(), hundredth(), etc to go alongside first() and second()?

**URL:** <https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 22, 2018, 8:23pm UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905 "2018-08-22T20:23:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 22, 2018, 8:23pm UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905/1 "2018-08-22T20:23:22Z")

</div>

So `first` is an extremely useful function, and so is `last`.

Some languages, like rails, even have a [`second()`](https://apidock.com/rails/ActiveRecord/FinderMethods/second) function. This has led to some controversy on the subject:

- [ruby - Rails lists have .first and .second – is there a .hundredth or .sixty\_nineth ? - Stack Overflow](https://stackoverflow.com/questions/7730605/rails-lists-have-first-and-second-is-there-a-hundredth-or-sixty-nineth)

* * *

What is the best way to define all the numbers from two to one thousand as functions that grab indices from arrays?

This might help a little?

- [https://rosettacode.org/wiki/Spelling\_of\_ordinal\_numbers#Julia](https://rosettacode.org/wiki/Spelling_of_ordinal_numbers#Julia)

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 22, 2018, 8:30pm UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905/2 "2018-08-22T20:30:37Z")

</div>

For the record, I think this is a terrible idea and nobody should do it. But, jut for fun

```julia
nthlist = ["first", "second", "third", "fourth", "fifth"]

for (n, nth) ∈ enumerate(Symbol.(nthlist))
    isdefined(@ __MODULE__ , nth) && continue
    @eval $nth(v) = v[$n] 
end

```

Of course, you’d have to do a little bit of English and string manipulation to generat `nthlist`. If you can find an existing list you’d be all set. It wouldn’t be hard to extend this to general iterators that don’t support indexing.

Seriously though, don’t do it.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [August 22, 2018, 9:07pm UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905/3 "2018-08-22T21:07:13Z")

</div>

Ah, the obviousness of 0-based indexing:

> <https://github.com/rails/rails/blob/17978719e4f1d3de6e3d1a00e4cd7a2116802f53/activesupport/lib/active_support/core_ext/array/access.rb#L75-L77>

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [August 22, 2018, 10:16pm UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905/4 "2018-08-22T22:16:38Z")

</div>

Wow, I’ve gotten so used to multiple dispatch that I stared at that code snippet for a few minutes in complete puzzlement before I realized what it meant. This coming from someone with about 8 years of mostly C++.

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [August 23, 2018, 12:22am UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905/5 "2018-08-23T00:22:40Z")

</div>

Obviously, you need to start with `zeroth`!

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 23, 2018, 5:39am UTC](https://discourse.julialang.org/t/easiest-way-to-make-tenth-hundredth-etc-to-go-alongside-first-and-second/13905/6 "2018-08-23T05:39:28Z")

</div>

FWIW, coming from Common Lisp I originally missed [`first`, …, `tenth`](http://clhs.lisp.se/Body/f_firstc.htm), but now I think that

1. In the (rare) cases I need it, `x -> x[n]` is more readable and should compile equally efficiently now (for a constant `n`),
2. More generally, using accessors like this encourages bad coding habits as it forces the user to remember the layout. This is a reasonable compromise for 2 elements, but for anything more, we have `NamedTuple`s (which are lightweight and yet optimized, ideal for most cases), composite types (for something you use more extensively), `Dict`s (for a heterogeneous number of elements), etc.
