Levi Aul
unread,Jul 18, 2014, 1:43:36 PM7/18/14Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to elixir-l...@googlegroups.com
There are two ways to think of a Range type:
1. A simple way to define a Stream that starts with the opening value (of a type that has a successor function defined), and produces successors until one matches the closing value. In this definition, Ranges are Enumerable, and can be converted to (finite, but maybe huge) Lists.
2. A compact representation of a functional predicate that does ordering comparisons. 0..1 is another way to say &((&1 >= 0) && (&1 <= 1)). In this definition, ranges can range over *continuous* quantities. For example, 0.1..0.5 selects all real numbers between 0.1 and 0.5.
Under definition #1, exclusive ranges are an extraneous concept, as you can pretty simply construct an exclusive range from the successor of the opening value and the antecedent of the closing value.
Under definition #2, though, exclusive ranges are how you specify continuous quantities that don't include their boundaries—in other words, comparisons which use > instead of >=.
If you still want a minimal range syntax, though, there's another way to encode exclusivity: define a "discontinuity decorator" which takes a value and a direction, and acts like the asymptote of that value "coming from" that direction, in the calculus sense. Example (presuming that epsilon is the smallest representable floating-point number):
zeroplus = inexact(0, :+)
true = (zeroplus > 0)
true = (zeroplus >= 0)
false = (zeroplus == 0)
false = (zeroplus <= 0)
false = (zeroplus < 0)
true = zeroplus > -epsilon
true = zeroplus < +epsilon
true = (zeroplus == zeroplus)
And then you could have your exclusive range like so:
# (0, 1] in mathematical notation
inexact(0, :+) .. 1
I'm not sure whether it'd be possible to make inexact() objects work on an general Erlang VM level, but Ranges (and maybe Streams) could certainly do special-case logic for them.