# Use of "end" in array element selection

**URL:** https://discourse.julialang.org/t/use-of-end-in-array-element-selection/41598
**Category:** Performance
**Created:** [June 17, 2020, 3:02pm UTC](https://discourse.julialang.org/t/use-of-end-in-array-element-selection/41598 "2020-06-17T15:02:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [June 17, 2020, 3:02pm UTC](https://discourse.julialang.org/t/use-of-end-in-array-element-selection/41598/1 "2020-06-17T15:02:42Z")

</div>

The `end` token means “last element of array”, and it is essentially equivalent to `-1` in Python. However, that is not always true. Consider the array:

```julia
a = collection(1:100)
b = view(a, 1:20)
c = view(a, 30:end)

```

I get the error:

```julia
ERROR: syntax: missing last argument in "30:" range expression 

```

Why was the decision taken not to use the `end` token consistently? Why allow it in an expression `a[20:end]` and not in `view(a,20:end)`? I know I can use `view(a, 20:length(a))`, but that is a heavy notation, and Julia prides itself in being terse and elegant.  
Thanks.

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [June 17, 2020, 3:13pm UTC](https://discourse.julialang.org/t/use-of-end-in-array-element-selection/41598/2 "2020-06-17T15:13:29Z")

</div>

It’s because as the argument of a function, the expression `30:end` is evaluated without Julia “knowing” that it represents indices of the previous argument. The “nice” notation can be used with the macro `@view`:

```julia
@view a[30:end]

```

(By the way, there is a mistake in your example: the function `collection` is not in `Base`; I think you meant `collect`.)

---

<div class="post-metadata">

### Author: ![r2cp](https://avatars.discourse-cdn.com/v4/letter/r/67e7ee/32.png) [@r2cp](https://discourse.julialang.org/u/r2cp)
#### Post date: [June 17, 2020, 3:21pm UTC](https://discourse.julialang.org/t/use-of-end-in-array-element-selection/41598/3 "2020-06-17T15:21:47Z")

</div>

Also, it might be useful: A reference to `X[end]` in slicing operations is really a call to `lastindex(X)`, so it makes sense this special keyword works only in the context of a slicing operation, rather than the call of `view` function.

[https://docs.julialang.org/en/v1/manual/interfaces/](https://docs.julialang.org/en/v1/manual/interfaces/)

---

<div class="post-metadata">

### Author: ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)
#### Post date: [June 17, 2020, 3:27pm UTC](https://discourse.julialang.org/t/use-of-end-in-array-element-selection/41598/4 "2020-06-17T15:27:36Z")

</div>

Yes, you are correct. `collection` was wrong. Thanks for the tip!
