# Range with end

**URL:** <https://discourse.julialang.org/t/range-with-end/46971>\
**Category:** General Usage\
**Created:** [September 21, 2020, 12:04am UTC](https://discourse.julialang.org/t/range-with-end/46971 "2020-09-21T00:04:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pauljurczak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pauljurczak/32/921_2.png) [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Post date:** [September 21, 2020, 12:04am UTC](https://discourse.julialang.org/t/range-with-end/46971/1 "2020-09-21T00:04:16Z")

</div>

This works as expected:

```julia
b = rand(10)
b[2:end]
view(b, 2:10)

```

but not this:

```julia
julia> view(b, 2:end)
ERROR: syntax: missing last argument in "1:" range expression 

```

Do I really have to use:

```julia
view(b, 2:length(b))

```

Is there is a more clever way to exclude the first element of a vector?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [September 21, 2020, 12:06am UTC](https://discourse.julialang.org/t/range-with-end/46971/2 "2020-09-21T00:06:33Z")

</div>

No, that’s the advantage of the macro form:

```julia
@view b[2:end]

```

---

<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:** [September 21, 2020, 12:09am UTC](https://discourse.julialang.org/t/range-with-end/46971/3 "2020-09-21T00:09:03Z")

</div>

The `end` keyword for the last index of an array only works inside `[...]` indexing expressions.

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [September 21, 2020, 12:13am UTC](https://discourse.julialang.org/t/range-with-end/46971/4 "2020-09-21T00:13:18Z")

</div>

Has there been an effort to make “end” more like a keyword that can get passed around? Or is this considered a bad idea?

ie:

```nohighlight
a = 2:end
b = [1,2,3,4,5]
c = b[a]

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 21, 2020, 12:16am UTC](https://discourse.julialang.org/t/range-with-end/46971/5 "2020-09-21T00:16:28Z")

</div>

One slight nuance is that `a[2:end]` is actually lowered to `getindex(a, 2:lastindex(a))`, not `length`, which is an important distinction for `OffsetArray`s or strings.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 21, 2020, 12:31am UTC](https://discourse.julialang.org/t/range-with-end/46971/6 "2020-09-21T00:31:35Z")

</div>

The problem is that we use `end` to denote the end of blocks, so treating it as a symbol everywhere would lead to a lot of parsing ambiguities. That’s also why you can’t use `begin ... end` blocks inside indexing expressions, for example.

Another problem with this approach is that you basically end up writing a small computer algebra system and you would need a way to trace through arbitrary functions, since you don’t know the last index of the array in advance. I implemented something like that in a more limited way as `tbegin` and `tend` in [CoolTensors.jl](https://github.com/simeonschaub/CoolTensors.jl), so it’s not impossible to make this work for the most common cases, but it’s very difficult and brittle to implement this in all generality, so it’s suitable for Base.

---

<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:** [September 21, 2020, 2:17am UTC](https://discourse.julialang.org/t/range-with-end/46971/7 "2020-09-21T02:17:30Z")

</div>

> [@anon92994695](#):
>
> Has there been an effort to make “end” more like a keyword that can get passed around? Or is this considered a bad idea?
> 
> ie:
> 
> ```julia
> a = 2:end
> b = [1,2,3,4,5]
> c = b[a]
> 
> ```

`end` is a keyword that is also used to end blocks so it would be hard to also parse it as an end-of-array indices marker in general contexts. However, it would certainly be possible to define an `END` object that behaves very similarly:

```julia
import Base: Colon, getindex
struct End; end # singleton
const END = End()
struct EndUnitRange
    start::Int
end
(::Colon)(start::Integer, ::End) = EndUnitRange(start)
getindex(A::Array, r::EndUnitRange) = A[r.start:end]
# ... and other AbstractRange methods, probably…

```

You can then do:

```julia
julia> a = 2:END;

julia> b = [1,2,3,4,5];

julia> c = b[a]
4-element Array{Int64,1}:
 2
 3
 4
 5

```

However, a problem with this is that such a `2:END` range object has little meaning except in `getindex` and a few similar contexts, and for all of these specialized methods would have to be defined — you can’t use it in most contexts where an ordinary range would work. (What is `length(a)`? What does `for x in a` mean?) So I’m not sure how useful it would be in practice. (But someone could certainly make a package for it if they felt otherwise.)

In contrast, the current `2:end` syntax is only available in `object[...]` contexts where the meaning of `end` is unambiguous. And since `end` is “lowered” to `lastindex(object)`, individual types don’t need to do anything special to support it.

---

<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:** [September 21, 2020, 6:09am UTC](https://discourse.julialang.org/t/range-with-end/46971/8 "2020-09-21T06:09:28Z")

</div>

> [@simeonschaub](#):
>
> you basically end up writing a small computer algebra system and you would need a way to trace through arbitrary functions, since you don’t know the last index of the array in advance

I think that just assigning `lastindex` etc to a variable and using it for arithmetic is always better in this case.
