# How to break apart a UnitRange?

**URL:** <https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415>\
**Category:** General Usage\
**Created:** [August 6, 2020, 11:58am UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415 "2020-08-06T11:58:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 6, 2020, 11:58am UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/1 "2020-08-06T11:58:10Z")

</div>

How would one break apart a `UnitRange`? From this example I’d like to create a `Vector` or `Array` that has 1,2,3,4,5,6 etc. in it, instead of 1:2, 3:4, 5:6

```julia
searching = DataFrame(A = 1:5:1000)
searching_specific = searching[:,:A]
inds = findall(isodd, searching_specific)
ranges = @views (:).(inds[1:end-1], inds[2:end] .- 1)

```

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [August 6, 2020, 12:00pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/2 "2020-08-06T12:00:52Z")

</div>

I think you are looking for `collect()`:

```julia
julia> collect(1:6)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 6, 2020, 12:12pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/3 "2020-08-06T12:12:34Z")

</div>

Thank you for your reply. `collect()` does not break apart the `Vector{UnitRange{Int64}}`. What I am using it for is more complicated - I have a `DataFrame` where I sort by `0`, then take every 4th element in the `Vector{UnitRange{Int64}}`. I’d like to be able to ‘explode’ or break apart those sorted elements.

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [August 6, 2020, 12:35pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/4 "2020-08-06T12:35:59Z")

</div>

```nohighlight
A = [1:2, 3:4, 5:6]
mapreduce(collect, vcat, A)

```

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [August 6, 2020, 12:39pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/5 "2020-08-06T12:39:43Z")

</div>

you dont even need the map here

```julia
julia> A = [1:2, 3:4, 5:6]
3-element Array{UnitRange{Int64},1}:
 1:2
 3:4
 5:6

julia> reduce(vcat, A)
6-element Array{Int64,1}:
 1
 2
 3
 4
 5
 6

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 6, 2020, 1:09pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/6 "2020-08-06T13:09:58Z")

</div>

> [@HelgavonLichtenstein](#):
>
> ```julia
> searching = DataFrame(A = 1:5:1000)
> searching_specific = searching[:,:A]
> inds = findall(isodd, searching_specific)
> ranges = @views (:).(inds[1:end-1], inds[2:end] .- 1)
> 
> ```

You can create your input data a lot more easily.

Instead you can write:

```julia
ranges = [i:i+1 for i in 1:2:197]

```

I’m just writing this to encourage you to simplify your questions more, by creating a _minimal_ example. In particular, not everyone has DataFrames, and cannot understand your question without installing it. (In fact, you just put some data into a DataFrame and then read it directly back.)

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 6, 2020, 1:10pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/7 "2020-08-06T13:10:57Z")

</div>

I see I choose poorly for my MWE.

What I mean is, I do not want the middle values between each `UnitRange`. For example, if I have 1:252, 1225:1475, 2303:2555, 3342:3594, 4383:4652 in my `UnitRange`, I would like to break it apart into 1,252,1225,1475,2303 etc.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 6, 2020, 1:17pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/8 "2020-08-06T13:17:54Z")

</div>

> [@HelgavonLichtenstein](#):
>
> 1:252, 1225:1475, 2303:2555, 3342:3594, 4383:4652

Hmm, this should work, though I don’t think it’s the most efficient way (it would be better to avoid creating the temporary arrays):

```julia
reduce(vcat, [first(x), last(x)] for x in ranges)

```

---

<div class="post-metadata">

**Author:** ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)\
**Post date:** [August 6, 2020, 1:20pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/9 "2020-08-06T13:20:24Z")

</div>

Thank you very much! I will use this for plotting.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [August 6, 2020, 5:14pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/10 "2020-08-06T17:14:08Z")

</div>

If anyone needs more performance for this task, you can create a lazy datastructure first and (if absolutely necessary) collect it

```julia
range_limits(ranges) = Iterators.Flatten((first(r), last(r)) for r in ranges) |> collect

```

some benchmarks:

```julia
julia> using BenchmarkTools
julia> range_limits_allocating(ranges) = reduce(vcat, [first(x), last(x)] for x in ranges)
range_limits_allocating (generic function with 1 method)

julia> @benchmark range_limits_allocating($([rand(1:1000):rand(1:1000) for i in 1:100]))
BenchmarkTools.Trial: 
  memory estimate: 99.25 KiB
  allocs estimate: 199
  --------------
  minimum time: 8.237 μs (0.00% GC)
  median time: 9.432 μs (0.00% GC)
  mean time: 11.586 μs (10.10% GC)
  maximum time: 724.096 μs (98.04% GC)
  --------------
  samples: 10000
  evals/sample: 1

julia> @benchmark range_limits($([rand(1:1000):rand(1:1000) for i in 1:100]))
BenchmarkTools.Trial: 
  memory estimate: 4.38 KiB
  allocs estimate: 11
  --------------
  minimum time: 1.145 μs (0.00% GC)
  median time: 1.307 μs (0.00% GC)
  mean time: 1.521 μs (6.75% GC)
  maximum time: 148.801 μs (97.83% GC)
  --------------
  samples: 10000
  evals/sample: 10

```

so for 100 ranges this gives a roughly 10x speed up. without the collect it’s basically instantanous bc nothing is actually calculated

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 6, 2020, 6:37pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/11 "2020-08-06T18:37:10Z")

</div>

> [@josuagrw](#):
>
> `range_limits(ranges) = Iterators.Flatten((first(r), last(r)) for r in ranges) |> collect`

That’s clever! I didn’t realize that would work. But I wonder if it’s more idiomatic to use the function, ‘flatten’, rather than the (undocumented) constructor ‘Flatten’.

---

<div class="post-metadata">

**Author:** ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)\
**Post date:** [August 6, 2020, 6:55pm UTC](https://discourse.julialang.org/t/how-to-break-apart-a-unitrange/44415/12 "2020-08-06T18:55:16Z")

</div>

Oh, yeah! That definitely makes more sense. For some reason I thought `Iterators.Flatten()` was the documented method for that
