ObjectRange Atom feed

Ranges represent an interval of values. The value type just needs to be “compatible,” that is, to implement a succ method letting us step from one value to the next (its successor).

Prototype provides such a method for Number and String, but you are of course welcome to implement useful semantics in your own objects, in order to enable ranges based on them.

ObjectRange mixes in Enumerable, which makes ranges very versatile. It takes care, however, to override the default code for include, to achieve better efficiency.

While ObjectRange does provide a constructor, the preferred way to obtain a range is to use the $R utility function, which is strictly equivalent (only way more concise to use).


The most common use of ranges is, undoubtedly, numerical:

$A($R(1, 5)).join(', ')
// -> '1, 2, 3, 4, 5'

$R(1, 5).zip(['one', 'two', 'three', 'four', 'five'], function(tuple) {
  return tuple.join(' = ');
// -> ['1 = one', '2 = two', '3 = three', '4 = four', '5 = five']

Be careful with String ranges: as described in its succ method, it does not use alphabetical boundaries, but goes all the way through the character table:

$A($R('a', 'e'))
// -> ['a', 'b', 'c', 'd', 'e'], no surprise there

$A($R('ax', 'ba'))
// -> Ouch!  Humongous array, starting as ['ax', 'ay', 'az', 'a{', 'a|', 'a}', 'a~'...]



include(value) -> Boolean

Determines whether the value is included in the range.