# How to concatenate ranges?

imagine you have some setup:

``````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,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?

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

julia> x_vec = [
linspace(x_raw,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, 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
``````

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`.