scala.collection.immutable

class Range

[source: scala/collection/immutable/Range.scala]

@serializable

@SerialVersionUID(7618862778670199309L)

class Range(val start : Int, val end : Int, val step : Int)
extends IndexedSeq[Int]

The Range class represents integer values in range [start;end) with non-zero step value step. It's a special case of an indexed sequence. For example:

     val r1 = 0 until 10
     val r2 = r1.start until r1.end by r1.step + 1
     println(r2.length) // = 5
  
Author
Martin Odersky
Version
2.8
Since
2.5
Direct Known Subclasses:
Range.Inclusive, Range.ByOne

Value Summary
lazy val length : Int
Returns the length of the sequence.
Method Summary
final def apply (idx : Int) : Int
Returns the elements at position `idx`
def by (step : Int) : Range
Create a new range with the start and end values of this range and a new step.
def contains (x : Int) : Boolean
protected def copy (start : Int, end : Int, step : Int) : Range
override final def drop (n : Int) : Range
Returns this traversable without its n first elements If this traversable has less than n elements, the empty traversable is returned.
override final def dropRight (n : Int) : Range
Returns the iterable wihtout its rightmost n elements.
override final def dropWhile (p : (Int) => Boolean) : Range
Returns the longest suffix of this traversable whose first element does not satisfy the predicate p.
override def equals (other : Any) : Boolean
The equality method defined in `AnyRef`.
override def foreach [U](f : (Int) => U) : Unit
Apply a function f to all elements of this iterable object.
def inclusive : Inclusive
Make range inclusive.
override final def init : Range
a traversable consisting of all elements of this traversable except the last one.
override final def isEmpty : Boolean
Does this iterable contain no elements?
def isInclusive : Boolean
protected def limit : Int
override final def reverse : Range
A sequence of type C consisting of all elements of this sequence in reverse order.
override final def slice (from : Int, until : Int) : Range
A sub-iterable starting at index `from` and extending up to (but not including) index `until`.
override final def span (p : (Int) => Boolean) : (Range, Range)
Returns a pair consisting of the longest prefix of the traversable whose elements all satisfy the given predicate, and the rest of the traversable.
override final def splitAt (n : Int) : (Range, Range)
Split the traversable at a given point and return the two parts thus created.
override final def take (n : Int) : Range
Return an iterable consisting only of the first n elements of this iterable, or else the whole iterable, if it has less than n elements.
override final def takeRight (n : Int) : Range
Returns the rightmost n elements from this iterable.
override final def takeWhile (p : (Int) => Boolean) : Range
Returns the longest prefix of this iterable whose elements satisfy the predicate p.
override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.
Methods inherited from IndexedSeq
companion
Methods inherited from IndexedSeqLike
thisCollection, toCollection, iterator, forall, exists, find, foldLeft, foldRight, reduceLeft, reduceRight, zip, zipWithIndex, head, tail, last, sameElements, copyToArray, lengthCompare, segmentLength, indexWhere, lastIndexWhere, reverseIterator, startsWith, endsWith, view, view
Methods inherited from SeqLike
size, isDefinedAt, prefixLength, indexWhere, findIndexOf, indexOf, indexOf, lastIndexOf, lastIndexOf, lastIndexWhere, reverseMap, reversedElements, startsWith, indexOfSeq, indexOfSeq, lastIndexOfSeq, lastIndexOfSeq, contains, union, diff, intersect, removeDuplicates, patch, updated, +:, :+, padTo, sortWith, sortWith, sortBy, toSeq, indices, hashCode, findLastIndexOf, equalsWith, containsSlice, projection
Methods inherited from PartialFunction
orElse, andThen
Methods inherited from Function1
compose
Methods inherited from IterableLike
elements, toIterable, zipAll, toStream, canEqual, first, firstOption
Methods inherited from GenericTraversableTemplate
newBuilder, genericBuilder, unzip, flatten, transpose
Methods inherited from TraversableLike
repr, nonEmpty, hasDefiniteSize, ++, ++, map, flatMap, filter, filterNot, partialMap, remove, partition, groupBy, count, /:, :\, reduceLeftOption, reduceRightOption, sum, product, min, max, headOption, lastOption, copyToBuffer, copyToArray, toArray, toList, toIndexedSeq, toSet, mkString, mkString, mkString, addString, addString, addString, stringPrefix, withFilter
Methods inherited from AnyRef
getClass, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Value Details
lazy val length : Int
Returns the length of the sequence.

Method Details
protected def copy(start : Int, end : Int, step : Int) : Range

def by(step : Int) : Range
Create a new range with the start and end values of this range and a new step.

def isInclusive : Boolean

protected def limit : Int

override def foreach[U](f : (Int) => U) : Unit
Apply a function f to all elements of this iterable object.
Parameters
f - A function that is applied for its side-effect to every element. The result (of arbitrary type U) of function `f` is discarded.
Notes
This method underlies the implementation of most other bulk operations. Implementing `foreach` with `iterator` is often suboptimal. So `foreach` should be overridden in concrete collection classes if a more efficient implementation is available.

override final def isEmpty : Boolean
Does this iterable contain no elements?

@inline

final def apply(idx : Int) : Int
Returns the elements at position `idx`

override final def take(n : Int) : Range
Return an iterable consisting only of the first n elements of this iterable, or else the whole iterable, if it has less than n elements.
Parameters
n - the number of elements to take
Notes
Might return different results for different runs, unless this iterable is ordered

override final def drop(n : Int) : Range
Returns this traversable without its n first elements If this traversable has less than n elements, the empty traversable is returned.
Parameters
n - the number of elements to drop
Returns
the new traversable
Notes
Might return different results for different runs, unless this traversable is ordered

override final def init : Range
a traversable consisting of all elements of this traversable except the last one.
Throws
Predef.UnsupportedOperationException - if the stream is empty.
Notes
Might return different results for different runs, unless this traversable is ordered

override final def slice(from : Int, until : Int) : Range
A sub-iterable starting at index `from` and extending up to (but not including) index `until`.
Notes
c.slice(from, to) is equivalent to (but possibly more efficient than) c.drop(from).take(to - from)
Might return different results for different runs, unless this iterable is ordered
Parameters
from - The index of the first element of the returned subsequence
until - The index of the element following the returned subsequence

override final def takeWhile(p : (Int) => Boolean) : Range
Returns the longest prefix of this iterable whose elements satisfy the predicate p.
Parameters
p - the test predicate.
Notes
Might return different results for different runs, unless this iterable is ordered

override final def dropWhile(p : (Int) => Boolean) : Range
Returns the longest suffix of this traversable whose first element does not satisfy the predicate p.
Parameters
p - the test predicate.
Notes
Might return different results for different runs, unless this traversable is ordered

override final def span(p : (Int) => Boolean) : (Range, Range)
Returns a pair consisting of the longest prefix of the traversable whose elements all satisfy the given predicate, and the rest of the traversable.
Parameters
p - the test predicate
Returns
a pair consisting of the longest prefix of the traversable whose elements all satisfy p, and the rest of the traversable.
Notes
Might return different results for different runs, unless this traversable is ordered

override final def splitAt(n : Int) : (Range, Range)
Split the traversable at a given point and return the two parts thus created.
Parameters
n - the position at which to split
Returns
a pair of traversables composed of the first n elements, and the other elements.
Notes
Might return different results for different runs, unless this traversable is ordered

override final def takeRight(n : Int) : Range
Returns the rightmost n elements from this iterable.
Parameters
n - the number of elements to take
Notes
Might return different results for different runs, unless this iterable is ordered

override final def dropRight(n : Int) : Range
Returns the iterable wihtout its rightmost n elements.
Parameters
n - the number of elements to take
Notes
Might return different results for different runs, unless this iterable is ordered

override final def reverse : Range
A sequence of type C consisting of all elements of this sequence in reverse order.

def inclusive : Inclusive
Make range inclusive.
Precondition
if (step > 0) end != MaxInt else end != MinInt

def contains(x : Int) : Boolean

override def equals(other : Any) : Boolean
The equality method defined in `AnyRef`.

override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.