# Idiomatic way to specify exclusive range?

**URL:** <https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024>\
**Category:** General Usage\
**Created:** [January 8, 2021, 3:26am UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024 "2021-01-08T03:26:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [January 8, 2021, 3:26am UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024/1 "2021-01-08T03:26:30Z")

</div>

The idiomatic way to specify a range from `a` to `b` inclusive is `a:b`. Is there an standard operator or idiom to express the range `a` to `b` in which `b` itself is excluded (something like `range()` in Python)? I’m planning to make some updates to the token system for sorted containers in DataStructures.jl that will introduce notation for exclusive ranges. I prefer to follow an idiom if one exists.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 8, 2021, 3:32am UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024/2 "2021-01-08T03:32:38Z")

</div>

`a:b-1` ? Seems easy enough to parse and reads nicely as from a to b minus one.

---

<div class="post-metadata">

**Author:** ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)\
**Post date:** [January 8, 2021, 4:23pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024/3 "2021-01-08T16:23:56Z")

</div>

This would be a great solution if I had time to implement and test performant operations `+(::Token,::Int)` and `-(::Token,::Int)`, but unfortunately, that is outside the scope of work I could hope to accomplish in January. If there is an idiomatic notation that addresses just this limited usage, that would be great. If not, I will make something up.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [January 8, 2021, 4:32pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024/4 "2021-01-08T16:32:19Z")

</div>

You could also go crazy and do something like:

```julia
julia> a↦b = a:b-1
↦ (generic function with 1 method)

```

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

```

However, array comprehension fails for some reason:

```julia
julia> [i for i in 1↦4]
syntax: expected "]"

Stacktrace:
 [1] top-level scope at In[12]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 8, 2021, 7:23pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024/5 "2021-01-08T19:23:27Z")

</div>

@tamasgal, what about:

```julia
a↦b = (a:b-1)

```

This does not work, as explained by @mbauman. But the similar function does not look too bad:

```julia
julia> ↦(a,b) = a:b-1
↦ (generic function with 1 method)

julia> [i for i in ↦(1,4)]
3-element Array{Int64,1}:
 1
 2
 3

```

---

<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:** [January 8, 2021, 8:14pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-specify-exclusive-range/53024/6 "2021-01-08T20:14:03Z")

</div>

The trouble with that comprehension is that `↦` has a very low precedence. The fix is `[i for i in (1↦4)]`. You’ll generally need to add parens in places where you wouldn’t need to with `:`. There aren’t many other operators with the same precedence as `:` — [IntervalSets](https://github.com/JuliaMath/IntervalSets.jl) uses `..`, but it’s also inclusive by default.

That said, intervals are perhaps the right mental model for the sort of operation I’m guessing you’re doing — and IntervalSets does have support for open intervals (it’s just the function `OpenInterval` or explicit `Interval{:closed,:open}`).

You could also add shorthands with one of these unicode operators which have the same precedence as `:` and `..` — `… ⁝ ⋮ ⋱ ⋰ ⋯` — but unfortunately none of them are very evocative of a half-open-on-one-side sort of thing.
