# + for Unit Range. Is my REPL broken?

**URL:** <https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 18, 2025, 7:11pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112 "2025-03-18T19:11:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eventine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eventine/32/215272_2.png) [@Eventine](https://discourse.julialang.org/u/Eventine)\
**Post date:** [March 18, 2025, 7:11pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/1 "2025-03-18T19:11:48Z")

</div>

Is this really not there:

```julia
methods(+,(UnitRange{Int},Int))
# 0 methods for generic function "+" from Base

```

or is my local REPL broken?  
This is what I had to add:

```julia
Base.:(+)(r::AbstractRange{T}, x::Real) where {T<:Real} = range(first(r)+x, step=step(r), length=length(r))

```

Also, why this does not work: `Vect[[1,3,5,10:15]]`? I had to create custom types to overload this behavior. Is there a performance reason to it? Why do I need to use vectors to retrive indexes instead of Tuples?

---

<div class="post-metadata">

**Author:** ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)\
**Post date:** [March 18, 2025, 7:17pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/2 "2025-03-18T19:17:02Z")

</div>

This was never defined I think for the same reason `+(AbstractVector,Int)` isn’t but this works

```julia
julia> (1:4) .+1
2:5

```

What do you mean by `Vect`

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 18, 2025, 7:25pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/3 "2025-03-18T19:25:57Z")

</div>

Nope, not broken; the lack of that method is **highly** intentional! But adding your own methods like that to `+` is indeed a very easy way to break your REPL 🙂

If you want to add a value to every value in an array, use broadcasting with `.+`.

Unlike some other languages, Julia draws a sharp distinction between an integer and an array of an integer. You can’t use them in the same places, nor can they seamlessly convert back-and-forth without manual intervention.

This is an intentional limitation that has a very significant payoff: in my experience the explicitness required — with either a broadcast `.` or something similar — ends up making code easier to read and less prone to unintentional errors.

---

<div class="post-metadata">

**Author:** ![Eventine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eventine/32/215272_2.png) [@Eventine](https://discourse.julialang.org/u/Eventine)\
**Post date:** [March 18, 2025, 7:51pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/4 "2025-03-18T19:51:50Z")

</div>

Ok, even thought for me it is still hard to grasp all of the intricacies.

I will try to lay out my problem better:

I have a “group” of indexes, probably a vector or a tuple, and they can be either Int, unitrange/steprange, or vector/tuple of int, e.g.:

```julia
1, 3, 5, 10:12 ....

```

I would like to extract values from a vector, something like

```julia
vect[[1,3,5,10:12]]

```

What is your suggested course of action?

Then what I would like to do, is to add an offset to that “group” of indexes.  
How should I approach the problem in Julia?

---

<div class="post-metadata">

**Author:** ![Eventine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eventine/32/215272_2.png) [@Eventine](https://discourse.julialang.org/u/Eventine)\
**Post date:** [March 18, 2025, 7:54pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/5 "2025-03-18T19:54:32Z")

</div>

I know that `(1:4) .+ 1` works, but nor `[1,3,5:9] .+1` and `(1,3,5:9).+1` work

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [March 18, 2025, 7:55pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/6 "2025-03-18T19:55:57Z")

</div>

It is never a good idea to put things with different types into the same vector. It usually leads to performance issues.

In your case, I propose to make all them ranges, i.e. replace say `5` with `5:5`. Then the type is homogeneous. You can’t do a “double broadcast” easily but a list comprehension doesn’t seem too bad:

```julia
indices = [1:3, 5:5, 7:9]
shifted = [I .+ 2 for I in indices]

```

---

<div class="post-metadata">

**Author:** ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)\
**Post date:** [March 18, 2025, 7:56pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/7 "2025-03-18T19:56:47Z")

</div>

something like this ?

```julia
julia> mapreduce(collect,vcat,[1,2,3:4,(4,5,6)])
7-element Vector{Int64}:
 1
 2
 3
 4
 4
 5
 6

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 18, 2025, 7:58pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/8 "2025-03-18T19:58:35Z")

</div>

> [@Eventine](#):
>
> I would like to extract values from a vector, something like
> 
> ```julia
> vect[[1,3,5,10:12]]
> 
> ```
> 
> What is your suggested course of action?

In this particular situation, I suggest `vect[[1;3;5;10:12]]`. There are more efficient possibilities and also probably some packages for doing this sort of thing, depending on your needs, but I would start with this.

You can offset these with `vect[[1;3;5;10:12] .+ offset]`.

---

<div class="post-metadata">

**Author:** ![Eventine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eventine/32/215272_2.png) [@Eventine](https://discourse.julialang.org/u/Eventine)\
**Post date:** [March 18, 2025, 8:00pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/9 "2025-03-18T20:00:26Z")

</div>

I agree on the fact that in this case tuple is better than vector. But since `vect[1,3,5]` and `vect[(1,3,5)]` does not work, but only `vect[[1,3,5]]`, I wanted to stay consistent with that.

The solution you propose is interesting, but cumbersome. I wanted to provide an easy interactable framework, and that will complicate it even further

---

<div class="post-metadata">

**Author:** ![Eventine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eventine/32/215272_2.png) [@Eventine](https://discourse.julialang.org/u/Eventine)\
**Post date:** [March 18, 2025, 8:03pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/10 "2025-03-18T20:03:04Z")

</div>

I didn’t know that with `;` would work, but not with `,`.  
The difference that comes to mind is that one is a column-vector and the other is a row vector, is there something else under it? (just trying to understand)

also the correct one is `vect[([1;3;5;10:12].+ offeset)]` but don’t ask me why

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 18, 2025, 8:10pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/11 "2025-03-18T20:10:54Z")

</div>

`[x;y;z]` is equivalent to `vcat(x,y,z)` which is vertical concatenation. It creates a result with as many rows as there exist total in all the inputs. This works in this case because it takes all of your collections-of-`Int`s and makes them into one larger collection-of-`Int`s, which is a valid index type.

`[x,y,z]` is a different operation, which creates a 3-element array where one entry is each of the inputs. Julia doesn’t have definitions for how to index in this way (for good reason, as it quickly becomes very ambiguous) which is why it didn’t work for indexing nor offsetting.

If we compare these different techniques, we see

```julia-repl
julia> [1;3;5;10:12]
6-element Vector{Int64}:
  1
  3
  5
 10
 11
 12

julia> [1,3,5,10:12]
4-element Vector{Any}:
 1
 3
 5
  10:12

```

---

<div class="post-metadata">

**Author:** ![Eventine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eventine/32/215272_2.png) [@Eventine](https://discourse.julialang.org/u/Eventine)\
**Post date:** [March 18, 2025, 8:14pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/12 "2025-03-18T20:14:37Z")

</div>

Ohh, basically it works because it collects them. I wanted to keep them as unitrange for memory implications.

I think I’ll stick with what I got for the vector (basically I created a new type of vector which interact nicely with this type of things), and for the offset, if overloading the + for UnitRange is dangerous as @mbauman was saying, I will just implement an offset! function.

Thanks everyone for the help!

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [March 18, 2025, 9:54pm UTC](https://discourse.julialang.org/t/for-unit-range-is-my-repl-broken/127112/13 "2025-03-18T21:54:12Z")

</div>

For what it’s worth, the following works:

```julia-repl
julia> (.+).([1,3,5:9], 1)
3-element Vector{Any}:
 2
 4
  6:10

```
