Base abstract class for VM assisted sequences.
Class Sequence
This class is meant to provide common methods to VM-assisted (that is, language level) sequence classes. You can't derive directly from this class unless you're writing a native module, but you can derive from script-visible classes children of this one.
Methods | |
append | Adds an item at the end of the sequence. |
back | Returns the last item in the Sequence. |
clear | Removes all the items from the Sequence. |
comp | Appends elements to this sequence through a filter. |
empty | Checks if the Sequence is empty or not. |
first | Returns an iterator to the first element of the Sequence. |
front | Returns the first item in the Sequence. |
last | Returns an iterator to the last element of the Sequence. |
mcomp | Appends elements to this sequence from multiple sources. |
mfcomp | Appends elements to this sequence from multiple sources through a filter. |
prepend | Adds an item in front of the sequence |
Adds an item at the end of the sequence.
Sequence.append( item )
item | The item to be added. |
If the sequence is sorted, the position at which the item is inserted is determined by the internal ordering; otherwise the item is appended at the end of the sequence.
Returns the last item in the Sequence.
Sequence.back()
Return | The last item in the Sequence. | ||
Raise |
|
This method overloads the BOM method back. If the Sequence is not empty, it returns the last element.
Removes all the items from the Sequence.
Sequence.clear()
Appends elements to this sequence through a filter.
Sequence.comp( source, [filter] )
source | A sequence, a range or a callable generating items. |
filter | A filtering function receiving one item at a time. |
Return | This sequence. |
This method adds one item at a time to this Sequence.
If source is a range, (must not be open), all the values generated by the range will be appended, considering range direction and step.
If source is a sequence (array, dictionary, or any other object providing the sequence interface), all the values in the item will be appended to this Sequence.
If source is a callable item, it is called repeatedly to generate the sequence. All the items it returns will be appended, until it declares being terminated by returning an oob(0). Continuation objects are also supported.
If a filter callable is provided, all the items that should be appended are first passed to to it; the item returned by the callable will be used instead of the item provided in source. The filter may return an out of band 1 to skip the item from source, or an out of band 0 to stop the processing altogether. The filter callable receives also the forming sequence as the second parameter so that it account for it or manage it dynamically during the filter step.
For example, the following comprehension creates a dictionary associating a letter of the alphabet to each element in the source sequence, discarding elements with spaces and terminating when a "
dict = [=>].comp( // the source .[ 'bananas' 'skip me' 'apples' 'oranges' '<end>' 'melons' ], // the filter { element, dict => if " " in element: return oob(1) if "<end>" == element: return oob(0) return [ "A" / len(dict), element ] // (1) } ) // (1): "A" / number == chr( ord("A") + number )
This method actually adds each item in the comprehension to the sequence or sequence-compatible item in self. This means that comprehension needs not to be performed on a new, empty sequence; it may be also used to integrate more data in already existing sequences.
See also: Array, Dictionary, Object.
Checks if the Sequence is empty or not.
Sequence.empty()
Return | True if the Sequence is empty, false if contains some elements. |
Returns an iterator to the first element of the Sequence.
Sequence.first()
Return | An iterator. |
Returns an iterator to the first element of the Sequence. If the Sequence is empty, an invalid iterator will be returned, but an insertion on that iterator will succeed and append an item to the Sequence.
Returns the first item in the Sequence.
Sequence.front()
Return | The first item in the Sequence. | ||
Raise |
|
This method overloads the BOM method front. If the Sequence is not empty, it returns the first element.
Returns an iterator to the last element of the Sequence.
Sequence.last()
Return | An iterator. |
Returns an iterator to the last element of the Sequence. If the Sequence is empty, an invalid iterator will be returned, but an insertion on that iterator will succeed and append an item to the Sequence.
Appends elements to this sequence from multiple sources.
Sequence.mcomp( ... )
... | One or more sequences, ranges or callables generating items. |
Return | This sequence. |
This method works as Sequence.comp but it's possible to specify more items and sequences. Each element in the result set is an array of items in which each element extracted from a source or returned by a generator is combined with all the others.
For example, the following operation:
[].mcomp( [1,2], [3,4] )
results in:
[ [1,3], [1,4], [2,3], [2,4] ]
Generators are called repeatedly until they exhaust all the items they can generate, in the same order as they are declared in the mcomp call.
For example:
[].mcomp( alphagen, betagen )
will first call alphagen until it returns an oob(0), and then call betagen repeatedly.
Note: Calls in this context are not atomic. Suspension, sleep, I/O, and continuations are allowed and supported.
After all the generators are called, the collected data is mixed with static data coming from other sources. For example:
function make_gen( count, name ) i = 0 return {=> if i == count: return oob(0) > name, ": ", i return i++ } end > [].mcomp( ["a","b"], make_gen(2, "first"), make_gen(2, "second") ).describe()
will generate the following output:
first: 0 first: 1 second: 0 second: 1 [ [ "a", 0, 0], [ "a", 0, 1], [ "a", 1, 0], [ "a", 1, 1], [ "b", 0, 0], [ "b", 0, 1], [ "b", 1, 0], [ "b", 1, 1]]
Note: The Sequence.mfcomp provides a more flexible approach.
See also: Array, Dictionary, Object.
Appends elements to this sequence from multiple sources through a filter.
Sequence.mfcomp( filter, ... )
filter | A filtering function receiving one item at a time. |
... | One or more sequences, ranges or callables generating items. |
Return | This sequence. |
This function works exactly as Sequence.mcomp, with the difference that the elements generated are passed to a filter function for final delivery to the target sequence.
Note: The filter parameter is optional; if nil is passed to it, this method works exactly as Sequence.mcomp.
For example, the math set operation
{ x*y for x in 1,2,3 and y in 4,5,6 }
can be written using mfcomp like the following:
[].mfcomp( {x, y => x*y}, .[1 2 3], .[4 5 6] )
which results in
[ 4, 5, 6, 8, 10, 12, 12, 15, 18]
The as for Sequence.comp, filter receives an extra parameter which is the sequence itself. For example, the following code:
> [].mfcomp( {x, y, seq => printl( "Seq is now long: " + seq.len() ) return [seq.len(), x*y] }, .[1 2 3], .[4 5 6] ).describe()
generates this output:
Seq is now long: 0 Seq is now long: 1 Seq is now long: 2 Seq is now long: 3 Seq is now long: 4 Seq is now long: 5 Seq is now long: 6 Seq is now long: 7 Seq is now long: 8 [ [ 0, 4], [ 1, 5], [ 2, 6], [ 3, 8], [ 4, 10], [ 5, 12], [ 6, 12], [ 7, 15], [ 8, 18]]
Notice that it is possible to modify the sequence inside the filter, in case it's needed.
The filter may return an oob(1) to skip the value, and an oob(0) to terminate the operation. For example, the following code s
Note: The call Sequence.mfcomp( filter, seq ) is equivalent to Sequence.comp( seq, filter ).
See also: Array, Dictionary, Object.
Adds an item in front of the sequence
Sequence.prepend( item )
item | The item to be added. |
If the sequence is sorted, the position at which the item is inserted is determined by the internal ordering; otherwise the item is prepended in front of the sequence.