Next: Ring buffer, Previous: Random data generators, Up: Library modules - Utilities [Contents][Index]
data.range
- RangeA range object is an immutable sequence with O(1) indexed access
to the elements, and each element may be computed procedurally.
For example, a range of intergers between 0 and N can be trivially
realized as a range, where i-th element is simply computed by
identity
. It is a lot more space-efficient than actually generating
a sequence containing every number.
It also allows certain operations efficient, such as taking subsequence or appending sequences.
A range object can be used with :range
qualifier in the
srfi-42 eager comprehension (see Eager comprehensions).
A portable range object interface is defined in srfi-196
.
We implement the range object as a <sequence>
, so that all
sequence framework interface can be used on the ranges
(see Sequence framework). We also provide some additional
procedures that are not in srfi-196.
Range object class. Internally we use several subclasses
according to the nature of the range, but externally all
ranges can be treated as an instance of <range>
.
The metaclass of <range>
class.
[SRFI-196]
{data.range}
Creates and returns a range of length length,
whose i-th element (0 <= i
< length))
is determined by a procedure indexer, which takes i
as the sole argument and returns the corresponding element.
Note that indexer must run in O(1) time, and must be referentially transparent. The impelemntation may “expand” the range, that is, computes the element values into a flat vector internally.
(range->list (range 5 (^i (+ i 10)))) ⇒ (10 11 12 13 14)
[SRFI-196] {data.range} Creates and returns a range of integers starting from start (inclusive) and ending below end (exclusive), increasing with step. The default value of step is 1.
(range->list (numeric-range 2 6)) ⇒ (2 3 4 5) (range->list (numeric-range 0 5 2/3)) ⇒ (0 2/3 4/3 2 8/3 10/3 4 14/3)
[SRFI-196] {data.range} Creates and returns a range of integers. Total length of the range is length. The range starts from start (default 0), and increased with step (default 1).
(range->list (iota-range 5)) ⇒ (0 1 2 3 4) (range->list (iota-range 7 1 -1/7)) ⇒ (1 6/7 5/7 4/7 3/7 2/7 1/7)
[SRFI-196+] {data.range} Returns a range over the given vector vec. The vector is kept in the range, so you shouldn’t mutate vec.
The optional start and end arguments limits the range of the vector to be used. They are Gauche’s extension and not in srfi-196.
(range->list (vector-range '#(a b c))) ⇒ (a b c) (range->list (vector-range '#(a b c d e) 1 4)) ⇒ (b c d)
{data.range} Returns a range over the given uniform vector uvec. The uniform vector is kept in the range, so you shouldn’t mutate uvec.
The optional start and end arguments limits the range of the uniorm vector to be used.
[SRFI-196] {data.range} Returns a range over each character in the given string str. The string is kept in the range, so you shouldn’t mutate str.
The optional start and end arguments limits the range of the vector to be used. They are Gauche’s extension and not in srfi-196.
(range->list (string-range "abc")) ⇒ (#\a #\b #\c) (range->list (string-range "abcde" 1 4)) ⇒ (#\b #\c #\d)
[SRFI-196] {data.range} Returns a new range that walks over concatenation of the given ranges.
[SRFI-196+] {data.range} Returns a new range that walks over the elements of range, but in reverse order.
The optional start and end arguments limits the range of the vector to be used. They are Gauche’s extension and not in srfi-196.
(range->list (range-reverse (string-range "abc"))) ⇒ (#\c #\b #\a) (range->list (range-reverse (string-range "abcdef" 1 4))) ⇒ (#\d #\c #\b)
[SRFI-196] {data.range} Returns true iff obj is a range.
[SRFI-196]
{data.range}
Returns true iff all ranges have the same length, and any pair
of corresponding elements in the given ranges are equal in terms of
elt=
predicate.
As edge cases, when zero or one range is given, #t
is returned.
(range=? eqv? (numeric-range 0 5) (iota-range 5) (vector-range '#(0 1 2 3 4))) ⇒ #t
[SRFI-196] {data.range} Returns the length of range.
[SRFI-196+] {data.range} Returns the i-th element of range. The index i must be an exact integer.
If i is negative, or greater than or equal to the length of range, fallback is returned if given, or an error is signaled. The fallback argument is Gauche’s extension and not in srfi-196.
[SRFI-196+] {data.range} Returns the first element of range.
If the range is empty, fallback is returned if given, or an error is signaled. The fallback argument is Gauche’s extension and not in srfi-196.
[SRFI-196+] {data.range} Returns the last element of range.
If the range is empty, fallback is returned if given, or an error is signaled. The fallback argument is Gauche’s extension and not in srfi-196.
[SRFI-196] {data.range} Returns two ranges, the first one with elements before k-th elements of range, and the second one with k-th elements and after of range.
[SRFI-196] {data.range} Returns a new range that contains start-th (inclusive) to end-th (exclusive) elements of range.
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
[SRFI-196] {data.range}
Next: Ring buffer, Previous: Random data generators, Up: Library modules - Utilities [Contents][Index]