2015-01-18 15:36:36 -08:00
|
|
|
^title Sequence Class
|
|
|
|
|
^category core
|
|
|
|
|
|
2015-01-22 20:58:22 -08:00
|
|
|
An abstract base class for any iterable object. Any class that implements the
|
|
|
|
|
core [iterator protocol][] can extend this to get a number of helpful methods.
|
|
|
|
|
|
|
|
|
|
[iterator protocol]: ../control-flow.html#the-iterator-protocol
|
2015-01-18 15:36:36 -08:00
|
|
|
|
2015-03-27 07:43:36 -07:00
|
|
|
## Methods
|
|
|
|
|
|
2015-01-18 15:36:36 -08:00
|
|
|
### **all**(predicate)
|
|
|
|
|
|
2015-01-22 20:58:22 -08:00
|
|
|
Tests whether all the elements in the sequence pass the `predicate`.
|
|
|
|
|
|
|
|
|
|
Iterates over the sequence, passing each element to the function `predicate`.
|
2015-03-28 10:18:45 -07:00
|
|
|
If it returns something [false](../control-flow.html#truth), stops iterating
|
|
|
|
|
and returns the value. Otherwise, returns `true`.
|
2015-01-22 20:58:22 -08:00
|
|
|
|
|
|
|
|
:::dart
|
|
|
|
|
[1, 2, 3].all {|n| n > 2} // False.
|
|
|
|
|
[1, 2, 3].all {|n| n < 4} // True.
|
|
|
|
|
|
2015-02-28 21:45:39 +01:00
|
|
|
### **any**(predicate)
|
|
|
|
|
|
|
|
|
|
Tests whether any element in the sequence passes the `predicate`.
|
|
|
|
|
|
|
|
|
|
Iterates over the sequence, passing each element to the function `predicate`.
|
2015-03-28 10:18:45 -07:00
|
|
|
If it returns something [true](../control-flow.html#truth), stops iterating and
|
|
|
|
|
returns that value. Otherwise, returns `false`.
|
2015-02-28 21:45:39 +01:00
|
|
|
|
|
|
|
|
:::dart
|
|
|
|
|
[1, 2, 3].any {|n| n < 1} // False.
|
|
|
|
|
[1, 2, 3].any {|n| n > 2} // True.
|
|
|
|
|
|
2015-03-15 15:43:10 +01:00
|
|
|
### **contains**(element)
|
|
|
|
|
|
|
|
|
|
Returns whether the sequence contains any element equal to the given element.
|
|
|
|
|
|
2015-03-14 14:17:21 +01:00
|
|
|
### **count**
|
|
|
|
|
|
|
|
|
|
The number of elements in the sequence.
|
|
|
|
|
|
|
|
|
|
Unless a more efficient override is available, this will iterate over the
|
|
|
|
|
sequence in order to determine how many elements it contains.
|
|
|
|
|
|
|
|
|
|
### **count**(predicate)
|
|
|
|
|
|
|
|
|
|
Returns the number of elements in the sequence that pass the `predicate`.
|
|
|
|
|
|
|
|
|
|
Iterates over the sequence, passing each element to the function `predicate`
|
|
|
|
|
and counting the number of times the returned value evaluates to `true`.
|
|
|
|
|
|
|
|
|
|
:::dart
|
|
|
|
|
[1, 2, 3].count {|n| n > 2} // 1.
|
|
|
|
|
[1, 2, 3].count {|n| n < 4} // 3.
|
|
|
|
|
|
2015-03-28 20:35:20 +01:00
|
|
|
### **each**(function)
|
|
|
|
|
|
|
|
|
|
Iterates over the sequence, passing each element to the given `function`.
|
|
|
|
|
|
|
|
|
|
:::dart
|
|
|
|
|
["one", "two", "three"].each {|word| IO.print(word) }
|
|
|
|
|
|
2015-01-23 20:45:23 -08:00
|
|
|
### **join**(sep)
|
|
|
|
|
|
|
|
|
|
Returns a string representation of the list. The string representations of the
|
|
|
|
|
elements in the list is concatenated with intervening occurrences of `sep`.
|
|
|
|
|
|
|
|
|
|
It is a runtime error if `sep` is not a string.
|
|
|
|
|
|
|
|
|
|
### **join**
|
|
|
|
|
|
|
|
|
|
Calls `join` with the empty string as the separator.
|
|
|
|
|
|
2015-03-27 22:59:58 +01:00
|
|
|
### **list**
|
|
|
|
|
|
|
|
|
|
Creates a [list](list.html) containing all the elements in the sequence.
|
|
|
|
|
|
|
|
|
|
:::dart
|
2015-04-01 07:31:15 -07:00
|
|
|
(1..3).list // [1, 2, 3].
|
|
|
|
|
|
|
|
|
|
If the sequence is already a list, this creates a copy of it.
|
2015-03-27 22:59:58 +01:00
|
|
|
|
2015-01-22 20:58:22 -08:00
|
|
|
### **map**(transformation)
|
|
|
|
|
|
2015-03-28 22:51:50 +01:00
|
|
|
Creates a new sequence that applies the `transformation` to each element in the
|
|
|
|
|
original sequence while it is iterated.
|
2015-01-22 20:58:22 -08:00
|
|
|
|
2015-04-01 07:31:15 -07:00
|
|
|
:::dart
|
|
|
|
|
var doubles = [1, 2, 3].map {|n| n * 2 }
|
|
|
|
|
for (n in doubles) {
|
|
|
|
|
IO.print(n) // "2", "4", "6".
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
The returned sequence is *lazy*. It only applies the mapping when you iterate
|
|
|
|
|
over the sequence, and it does so by holding a reference to the original
|
|
|
|
|
sequence.
|
|
|
|
|
|
|
|
|
|
This means you can use `map(_)` for things like infinite sequences or sequences
|
|
|
|
|
that have side effects when you iterate over them. But it also means that
|
|
|
|
|
changes to the original sequence will be reflected in the mapped sequence.
|
|
|
|
|
|
|
|
|
|
To force eager evaluation, just call `.list` on the result.
|
2015-01-22 20:58:22 -08:00
|
|
|
|
|
|
|
|
:::dart
|
2015-04-01 07:31:15 -07:00
|
|
|
var numbers = [1, 2, 3]
|
|
|
|
|
var doubles = numbers.map {|n| n * 2 }.list
|
|
|
|
|
numbers.add(4)
|
|
|
|
|
IO.print(doubles) // [2, 4, 6].
|
2015-01-18 15:36:36 -08:00
|
|
|
|
|
|
|
|
### **reduce**(function)
|
|
|
|
|
|
2015-04-01 07:31:15 -07:00
|
|
|
Reduces the sequence down to a single value. `function` is a function that takes
|
|
|
|
|
two arguments, the accumulator and sequence item and returns the new accumulator
|
|
|
|
|
value. The accumulator is initialized from the first item in the sequence. Then,
|
|
|
|
|
the function is invoked on each remaining item in the sequence, iteratively
|
|
|
|
|
updating the accumulator.
|
2015-01-18 15:36:36 -08:00
|
|
|
|
|
|
|
|
It is a runtime error to call this on an empty sequence.
|
|
|
|
|
|
|
|
|
|
### **reduce**(seed, function)
|
|
|
|
|
|
2015-04-01 07:31:15 -07:00
|
|
|
Similar to above, but uses `seed` for the initial value of the accumulator. If
|
|
|
|
|
the sequence is empty, returns `seed`.
|
2015-01-22 20:58:22 -08:00
|
|
|
|
|
|
|
|
### **where**(predicate)
|
|
|
|
|
|
2015-03-28 22:51:50 +01:00
|
|
|
Creates a new sequence containing only the elements from the original sequence
|
|
|
|
|
that pass the `predicate`.
|
2015-01-22 20:58:22 -08:00
|
|
|
|
2015-03-28 22:51:50 +01:00
|
|
|
During iteration, each element in the original sequence is passed to the
|
|
|
|
|
function `predicate`. If it returns `false`, the element is skipped.
|
2015-01-22 20:58:22 -08:00
|
|
|
|
2015-04-01 07:31:15 -07:00
|
|
|
:::dart
|
|
|
|
|
var odds = (1..10).where {|n| n % 2 == 1 }
|
|
|
|
|
for (n in odds) {
|
|
|
|
|
IO.print(n) // "1", "3", "5", "7", "9".
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
The returned sequence is *lazy*. It only applies the filtering when you iterate
|
|
|
|
|
over the sequence, and it does so by holding a reference to the original
|
|
|
|
|
sequence.
|
|
|
|
|
|
|
|
|
|
This means you can use `where(_)` for things like infinite sequences or
|
|
|
|
|
sequences that have side effects when you iterate over them. But it also means
|
|
|
|
|
that changes to the original sequence will be reflected in the filtered
|
|
|
|
|
sequence.
|
|
|
|
|
|
|
|
|
|
To force eager evaluation, just call `.list` on the result.
|
2015-03-28 22:51:50 +01:00
|
|
|
|
|
|
|
|
:::dart
|
2015-04-01 07:31:15 -07:00
|
|
|
var numbers = [1, 2, 3, 4, 5, 6]
|
|
|
|
|
var odds = numbers.where {|n| n % 2 == 1 }.list
|
|
|
|
|
numbers.add(7)
|
|
|
|
|
IO.print(odds) // [1, 3, 5].
|