# Lazy subranges or \`1:end\` as entity

**URL:** https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159
**Category:** General Usage
**Created:** [May 26, 2018, 6:27am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159 "2018-05-26T06:27:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [May 26, 2018, 6:27am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159/1 "2018-05-26T06:27:07Z")

</div>

Sometimes I’d like to be able to write

```julia
r = 1:End()-1
x[r] .= y[r]

```

Is this a good idea and how to achieve this?

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 26, 2018, 6:41am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159/2 "2018-05-26T06:41:43Z")

</div>

Are you concerned by the cost of building the `1:end - 1` range in:

```julia
x[1:end - 1] .= y[1:end - 1]

```

?

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [May 26, 2018, 7:18am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159/3 "2018-05-26T07:18:02Z")

</div>

No, not at all, just for readability in expressions like

```julia
sk=10; plot([first.(y[1:sk:end]) for y in ys[1:sk:end]],[last.(y[1:sk:end,end]) for y in ys[1:sk:end]])

```

vs

```julia
r=1:10:End()
plot([first.(y[r, end]) for y in ys[r]], [last.(y[r,end]) for y in ys[r]])

```

---

<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: [May 26, 2018, 7:41am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159/4 "2018-05-26T07:41:14Z")

</div>

You could define

1. an `End` type, eg describing `end + a`,
2. arithmetic on it,
3. a `Base.colon` method, that emits another custom type which can contains ranges with this kind of endpoints,
4. `Base.getindex` and `Base.setindex` method for this custom type.

I don’t think it is worth it though. If in the above example you don’t want `End` to resolve differently for `x` and `y` and they have the same indexes, you could use something like

```julia
r = indices(x, 1)[1:(end-1)]
x[r] .= y[r]

```

Otherwise, I think that `x[1:(end-1)] .= y[1:(end-1)]` is just more readable.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [May 26, 2018, 7:45am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159/5 "2018-05-26T07:45:03Z")

</div>

I once (in Julia 0.2 or 0.3) made such an `End` type: [Bitbucket](https://bitbucket.org/maurow/ragged.jl/src/ebbd1cdfd796ef626e9c5193dc75b6494fe8b21f/src/Ragged.jl#lines-13)  
But I’m sure there are better ways to go about it. But it’s not trivial, that is why Julia opted for a syntax approach to the problem.

---

<div class="post-metadata">

### Author: ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)
#### Post date: [May 26, 2018, 10:40am UTC](https://discourse.julialang.org/t/lazy-subranges-or-1-end-as-entity/11159/6 "2018-05-26T10:40:06Z")

</div>

I could also image to define something along the lines

```julia
struct Formula{T} f::T end
farg(args, symb) = last(args[findlast(x->first(x)==symb, args)])
import Base.getindex
(f::Formula)(;args...) = f.f(;args...)
getindex(a::AbstractArray, f::Formula) = a[f(END=length(a))]

```

so that for

```julia
r = Formula((;args...)->1:farg(args, :END)-1)

```

```julia
julia> rand(10)[r]
9-element Array{Float64,1}:
 0.355413 
 0.26395  
 0.260973 
 0.0454235
 0.860611 
 0.958574 
 0.912541 
 0.812007 
 0.500342 

```

and then define a macro `@formula` giving syntactic sugar to `r = Formula((;args...)->1:farg(args, :END)-1)`
