[Proposal] Range.contains?

65 views
Skip to first unread message

Patrick Davey

unread,
Dec 20, 2016, 4:43:07 PM12/20/16
to elixir-lang-core
I wanted to check if a range contained another range today while I was doing one of the www.adventofcode.com puzzles.

I discussed it on the forum and I have done a search and it doesn't look like it has been proposed before, apologies if I have missed something.

Anyway, just an idea, I'm happy to have a go at implementing it if it's something which merits approval. I understand if it's too simple a thing to be bothered implementing.

Thanks to the team for Elixir - I'm thoroughly enjoying getting more comfortable with it through the advent of code puzzles :)

Drew Olson

unread,
Dec 20, 2016, 4:46:51 PM12/20/16
to elixir-l...@googlegroups.com
I believe you want `Enum.member?(1..10, 1)`

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/62203f2a-fe01-46e8-a373-16f40b860578%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

José Valim

unread,
Dec 20, 2016, 4:47:40 PM12/20/16
to elixir-l...@googlegroups.com
Is there any reason to use Range.contains? given you can already do "x in range" or "Enum.member?(range, x)" (they are equivalent).



José Valim
Skype: jv.ptec
Founder and Director of R&D

--

Paul Schoenfelder

unread,
Dec 20, 2016, 4:53:39 PM12/20/16
to elixir-l...@googlegroups.com
I think he is looking for a Range function which answers whether a range is a subset of another range, e.g. 1..10 contains 2..4 == true.

José Valim

unread,
Dec 20, 2016, 4:55:54 PM12/20/16
to elixir-l...@googlegroups.com
If so, it may be a good addition but a better name is required, such as overlaps? or subset?.



José Valim
Skype: jv.ptec
Founder and Director of R&D

Paul Schoenfelder

unread,
Dec 20, 2016, 4:59:03 PM12/20/16
to elixir-l...@googlegroups.com
I like the idea of instead implementing Range.subset?/2 similar to MapSet.subset?/2.

Drew Olson

unread,
Dec 20, 2016, 4:59:07 PM12/20/16
to elixir-l...@googlegroups.com
I was working on the same problem today and would have also found it useful to have something like `Range.first/1` and `Range.last/1` for accessing the inclusive bounds (these are probably terrible names).

Paul Schoenfelder

unread,
Dec 20, 2016, 5:00:55 PM12/20/16
to elixir-l...@googlegroups.com
Drew, you can use r.first and r.last, where r is the binding for the range in question. Ranges are a struct with first and last fields.

José Valim

unread,
Dec 20, 2016, 5:01:38 PM12/20/16
to elixir-l...@googlegroups.com
You can also pattern match on the range first and last:

first .. last = range



José Valim
Skype: jv.ptec
Founder and Director of R&D

Drew Olson

unread,
Dec 20, 2016, 5:05:58 PM12/20/16
to elixir-l...@googlegroups.com
Thanks Jose and Paul, I'll be sure to take a closer look at the documented struct types in the future.

eksperimental

unread,
Dec 20, 2016, 5:07:14 PM12/20/16
to elixir-l...@googlegroups.com
I proposed this in the past. Code is still valid today as very little changes have been done to the
Range module.

Date: Mon, 15 Feb 2016 12:23:39 +0700
Reply-To: elixir-l...@googlegroups.com
Sender: elixir-l...@googlegroups.com

I have created a set of functions for the range module.
Initially, I based my work in functions written by "jdl"
https://github.com/mykewould/crutches/blob/ddf5535d66601f9630bee1807ef38cb57f52b9b4/lib/crutches/range.ex

You can have a look at the module and tests:
https://github.com/eksperimental/elixir/blob/range/lib/elixir/lib/range.ex
https://github.com/eksperimental/elixir/blob/range/lib/elixir/test/elixir/range_test.exs

New functions are: congruent?/2, disjoint?2, contiguous?/2,
intersection/2, member?/2, overlaps?/2, reverse/1, size/1, sort/2,
subset?/2, to_list/2, union/2

Some functions have been taken from MapSet, and other apply exclusively
to Range.

Please let me know what you think of this.

And if there is an interest in adding this Elixir, I will have to
contact the author of the initial functions for release permission.

Cheers



On Tue, 20 Dec 2016 16:00:33 -0600
Paul Schoenfelder <paulscho...@gmail.com> wrote:

> Drew, you can use r.first and r.last, where r is the binding for the range
> in question. Ranges are a struct with first and last fields.
>
> On Tue, Dec 20, 2016 at 3:59 PM, Drew Olson <dr...@drewolson.org> wrote:
>
> > I was working on the same problem today and would have also found it
> > useful to have something like `Range.first/1` and `Range.last/1` for
> > accessing the inclusive bounds (these are probably terrible names).
> >
> > On Tue, Dec 20, 2016 at 3:55 PM, José Valim <jose....@plataformatec.com.
> > br> wrote:
> >
> >> If so, it may be a good addition but a better name is required, such as
> >> overlaps? or subset?.
> >>
> >>
> >>
> >> *José Valim*
> >> www.plataformatec.com.br
> >> Skype: jv.ptec
> >> Founder and Director of R&D
> >>
> >> On Tue, Dec 20, 2016 at 10:53 PM, Paul Schoenfelder <
> >> paulscho...@gmail.com> wrote:
> >>
> >>> I think he is looking for a Range function which answers whether a range
> >>> is a subset of another range, e.g. 1..10 contains 2..4 == true.
> >>>
> >>> On Tue, Dec 20, 2016 at 3:47 PM, José Valim <
> >>> jose....@plataformatec.com.br> wrote:
> >>>
> >>>> Is there any reason to use Range.contains? given you can already do "x
> >>>> in range" or "Enum.member?(range, x)" (they are equivalent).
> >>>>
> >>>>
> >>>>
> >>>> *José Valim*
> >>>> www.plataformatec.com.br
> >>>> Skype: jv.ptec
> >>>> Founder and Director of R&D
> >>>>
> >>>> On Tue, Dec 20, 2016 at 10:43 PM, Patrick Davey <
> >>>> patric...@gmail.com> wrote:
> >>>>
> >>>>> I wanted to check if a range contained another range today while I was
> >>>>> doing one of the www.adventofcode.com puzzles.
> >>>>>
> >>>>> I discussed it on the forum and I have done a search and it doesn't
> >>>>> look like it has been proposed before, apologies if I have missed something.
> >>>>>
> >>>>> Anyway, just an idea, I'm happy to have a go at implementing it if
> >>>>> it's something which merits approval. I understand if it's too simple a
> >>>>> thing to be bothered implementing.
> >>>>>
> >>>>> Thanks to the team for Elixir - I'm thoroughly enjoying getting more
> >>>>> comfortable with it through the advent of code puzzles :)
> >>>>>
> >>>>> --
> >>>>> You received this message because you are subscribed to the Google
> >>>>> Groups "elixir-lang-core" group.
> >>>>> To unsubscribe from this group and stop receiving emails from it, send
> >>>>> an email to elixir-lang-co...@googlegroups.com.
> >>>>> To view this discussion on the web visit
> >>>>> https://groups.google.com/d/msgid/elixir-lang-core/62203f2a-
> >>>>> fe01-46e8-a373-16f40b860578%40googlegroups.com
> >>>>> <https://groups.google.com/d/msgid/elixir-lang-core/62203f2a-fe01-46e8-a373-16f40b860578%40googlegroups.com?utm_medium=email&utm_source=footer>
> >>>>> .
> >>>>> For more options, visit https://groups.google.com/d/optout.
> >>>>>
> >>>>
> >>>> --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "elixir-lang-core" group.
> >>>> To unsubscribe from this group and stop receiving emails from it, send
> >>>> an email to elixir-lang-co...@googlegroups.com.
> >>>> To view this discussion on the web visit https://groups.google.com/d/ms
> >>>> gid/elixir-lang-core/CAGnRm4K0Rk2uMJVTCNQi9t7nHD%3DHSsY4YRt_
> >>>> qEhTt6T4cot%3DPQ%40mail.gmail.com
> >>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K0Rk2uMJVTCNQi9t7nHD%3DHSsY4YRt_qEhTt6T4cot%3DPQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> >>>> .
> >>>>
> >>>> For more options, visit https://groups.google.com/d/optout.
> >>>>
> >>>
> >>> --
> >>> You received this message because you are subscribed to the Google
> >>> Groups "elixir-lang-core" group.
> >>> To unsubscribe from this group and stop receiving emails from it, send
> >>> an email to elixir-lang-co...@googlegroups.com.
> >>> To view this discussion on the web visit https://groups.google.com/d/ms
> >>> gid/elixir-lang-core/CAK%3D%2B-TtyLV_yYN9yB6ESP85LOQg%3Dj9KZ
> >>> MqT1FC8YBAxSActRXw%40mail.gmail.com
> >>> <https://groups.google.com/d/msgid/elixir-lang-core/CAK%3D%2B-TtyLV_yYN9yB6ESP85LOQg%3Dj9KZMqT1FC8YBAxSActRXw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> >>> .
> >>> For more options, visit https://groups.google.com/d/optout.
> >>>
> >>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "elixir-lang-core" group.
> >> To unsubscribe from this group and stop receiving emails from it, send an
> >> email to elixir-lang-co...@googlegroups.com.
> >> To view this discussion on the web visit https://groups.google.com/d/ms
> >> gid/elixir-lang-core/CAGnRm4LPbAK5UkMne9K-z4RJt3%2BfL3J3oQpc
> >> -B6nUR%2B-c04BZA%40mail.gmail.com
> >> <https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4LPbAK5UkMne9K-z4RJt3%2BfL3J3oQpc-B6nUR%2B-c04BZA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> >> .
> >>
> >> For more options, visit https://groups.google.com/d/optout.
> >>
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "elixir-lang-core" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to elixir-lang-co...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/
> > msgid/elixir-lang-core/CAN3B1jd8XHGCJo%3D5ChJo9OyeLRA5D-
> > iMdW3FmvGOz4o_%2B6f_%3DQ%40mail.gmail.com
> > <https://groups.google.com/d/msgid/elixir-lang-core/CAN3B1jd8XHGCJo%3D5ChJo9OyeLRA5D-iMdW3FmvGOz4o_%2B6f_%3DQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> > .

Aleksei Magusev

unread,
Dec 21, 2016, 3:55:04 PM12/21/16
to elixir-lang-core
I believe we can wait with addition of such function in stdlib until more use cases arise besides code puzzle.
Another reason for waiting is that the code for checking this looks fairly straightforward:

  first1..last1 = range1
  first2..last2 = range2
  first1 >= first2 and last1 <= last2
Reply all
Reply to author
Forward
0 new messages