# How to concatenate ranges?

**URL:** <https://discourse.julialang.org/t/how-to-concatenate-ranges/23673>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 29, 2019, 11:14pm UTC](https://discourse.julialang.org/t/how-to-concatenate-ranges/23673 "2019-04-29T23:14:25Z")\
**Posts on this page:** 3\
**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:** [April 29, 2019, 11:14pm UTC](https://discourse.julialang.org/t/how-to-concatenate-ranges/23673/1 "2019-04-29T23:14:25Z")

</div>

imagine you have some setup:

```julia
linspace(a,b,n) = range(a,stop=b,length=n)

n = 41
x_raw = linspace(-1,+1,n)

x_vec = [
  linspace(x_raw[1:2]...,3),
  linspace(x_raw[3],x_raw[end-2],n-4),
  linspace(x_raw[end-1:end]...,3)
]

```

Is there a way to make `x_vec` into another `AbstractRange` that joins those three linspacings?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 29, 2019, 11:55pm UTC](https://discourse.julialang.org/t/how-to-concatenate-ranges/23673/2 "2019-04-29T23:55:36Z")

</div>

```julia
julia> linspace(a,b,n) = range(a,stop=b,length=n)

julia> x_vec = [
         linspace(x_raw[3],x_raw[end-2],n-4),
         linspace(x_raw[end-1:end]...,3), 
         linspace(x_raw[1:2]...,3)
       ]
3-element Array{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},1}:
 -0.9:0.05:0.9
 0.95:0.025:1.0
 -1.0:0.025:-0.95

julia> function range_from_linspaces(xs)
           u = union(sort(xs)...)
           step = round(minimum(diff(u)), digits=8)
           return range(u[1], u[end], step=step)
       end
range_from_linspaces (generic function with 1 method)

julia> range_from_linspaces(x_vec)
-1.0:0.025:1.0

```

---

<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:** [April 30, 2019, 6:21am UTC](https://discourse.julialang.org/t/how-to-concatenate-ranges/23673/3 "2019-04-30T06:21:42Z")

</div>

> [@djsegal](#):
>
> Is there a way to make `x_vec` into another `AbstractRange` that joins those three linspacings?

Sure, with a custom function that verifies that they join in a compatible way (the gap is the common stepsize). Write it for a 2-argument version and use with `reduce`.
