2015-01-18 23:36:36 +00:00
|
|
|
^title Sequence Class
|
|
|
|
|
|
2015-01-23 04:58:22 +00: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.
|
|
|
|
|
|
2015-11-08 21:31:22 +00:00
|
|
|
[iterator protocol]: ../../control-flow.html#the-iterator-protocol
|
2015-01-18 23:36:36 +00:00
|
|
|
|
2015-03-27 14:43:36 +00:00
|
|
|
## Methods
|
|
|
|
|
|
2015-01-18 23:36:36 +00:00
|
|
|
### **all**(predicate)
|
|
|
|
|
|
2015-01-23 04:58:22 +00: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 17:18:45 +00:00
|
|
|
If it returns something [false](../control-flow.html#truth), stops iterating
|
|
|
|
|
and returns the value. Otherwise, returns `true`.
|
2015-01-23 04:58:22 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print([1, 2, 3].all {|n| n > 2}) //> false
|
|
|
|
|
System.print([1, 2, 3].all {|n| n < 4}) //> true
|
2015-01-23 04:58:22 +00:00
|
|
|
|
2015-02-28 20:45:39 +00: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-11-08 21:31:22 +00:00
|
|
|
If it returns something [true][], stops iterating and
|
2015-03-28 17:18:45 +00:00
|
|
|
returns that value. Otherwise, returns `false`.
|
2015-02-28 20:45:39 +00:00
|
|
|
|
2015-11-08 21:31:22 +00:00
|
|
|
[true]: ../../control-flow.html#truth
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print([1, 2, 3].any {|n| n < 1}) //> false
|
|
|
|
|
System.print([1, 2, 3].any {|n| n > 2}) //> true
|
2015-02-28 20:45:39 +00:00
|
|
|
|
2015-03-15 14:43:10 +00:00
|
|
|
### **contains**(element)
|
|
|
|
|
|
|
|
|
|
Returns whether the sequence contains any element equal to the given element.
|
|
|
|
|
|
2015-03-14 13:17:21 +00: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`.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print([1, 2, 3].count {|n| n > 2}) //> 1
|
|
|
|
|
System.print([1, 2, 3].count {|n| n < 4}) //> 3
|
2015-03-14 13:17:21 +00:00
|
|
|
|
2015-03-28 19:35:20 +00:00
|
|
|
### **each**(function)
|
|
|
|
|
|
|
|
|
|
Iterates over the sequence, passing each element to the given `function`.
|
|
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-09-15 14:46:09 +00:00
|
|
|
["one", "two", "three"].each {|word| System.print(word) }
|
2015-03-28 19:35:20 +00:00
|
|
|
|
2015-06-30 13:52:29 +00:00
|
|
|
### **isEmpty**
|
|
|
|
|
|
|
|
|
|
Returns whether the sequence contains any elements.
|
|
|
|
|
|
|
|
|
|
This can be more efficient that `count == 0` because this does not iterate over
|
|
|
|
|
the entire sequence.
|
|
|
|
|
|
2015-09-16 14:15:48 +00:00
|
|
|
### **join**(separator)
|
2015-01-24 04:45:23 +00:00
|
|
|
|
2015-09-16 14:15:48 +00:00
|
|
|
Converts every element in the sequence to a string and then joins the results
|
|
|
|
|
together into a single string, each separated by `separator`.
|
2015-01-24 04:45:23 +00:00
|
|
|
|
2015-09-16 14:15:48 +00:00
|
|
|
It is a runtime error if `separator` is not a string.
|
2015-01-24 04:45:23 +00:00
|
|
|
|
2015-09-16 14:15:48 +00:00
|
|
|
### **join**()
|
2015-01-24 04:45:23 +00:00
|
|
|
|
2015-09-16 14:15:48 +00:00
|
|
|
Converts every element in the sequence to a string and then joins the results
|
|
|
|
|
together into a single string.
|
2015-01-24 04:45:23 +00:00
|
|
|
|
2015-01-23 04:58:22 +00:00
|
|
|
### **map**(transformation)
|
|
|
|
|
|
2015-03-28 21:51:50 +00:00
|
|
|
Creates a new sequence that applies the `transformation` to each element in the
|
|
|
|
|
original sequence while it is iterated.
|
2015-01-23 04:58:22 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-04-01 14:31:15 +00:00
|
|
|
var doubles = [1, 2, 3].map {|n| n * 2 }
|
|
|
|
|
for (n in doubles) {
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print(n) //> 2
|
|
|
|
|
//> 4
|
|
|
|
|
//> 6
|
2015-04-01 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2015-04-06 15:21:32 +00:00
|
|
|
To force eager evaluation, just call `.toList` on the result.
|
2015-01-23 04:58:22 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-04-01 14:31:15 +00:00
|
|
|
var numbers = [1, 2, 3]
|
2015-04-06 15:21:32 +00:00
|
|
|
var doubles = numbers.map {|n| n * 2 }.toList
|
2015-04-01 14:31:15 +00:00
|
|
|
numbers.add(4)
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print(doubles) //> [2, 4, 6]
|
2015-01-18 23:36:36 +00:00
|
|
|
|
|
|
|
|
### **reduce**(function)
|
|
|
|
|
|
2015-07-10 16:18:22 +00: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 23:36:36 +00:00
|
|
|
|
|
|
|
|
It is a runtime error to call this on an empty sequence.
|
|
|
|
|
|
|
|
|
|
### **reduce**(seed, function)
|
|
|
|
|
|
2015-04-01 14:31:15 +00:00
|
|
|
Similar to above, but uses `seed` for the initial value of the accumulator. If
|
|
|
|
|
the sequence is empty, returns `seed`.
|
2015-01-23 04:58:22 +00:00
|
|
|
|
2015-04-06 15:21:32 +00:00
|
|
|
### **toList**
|
|
|
|
|
|
2015-11-08 21:31:22 +00:00
|
|
|
Creates a [list][] containing all the elements in the sequence.
|
|
|
|
|
|
|
|
|
|
[list]: list.html
|
2015-04-06 15:21:32 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print((1..3).toList) //> [1, 2, 3]
|
2015-04-06 15:21:32 +00:00
|
|
|
|
|
|
|
|
If the sequence is already a list, this creates a copy of it.
|
|
|
|
|
|
2015-01-23 04:58:22 +00:00
|
|
|
### **where**(predicate)
|
|
|
|
|
|
2015-03-28 21:51:50 +00:00
|
|
|
Creates a new sequence containing only the elements from the original sequence
|
|
|
|
|
that pass the `predicate`.
|
2015-01-23 04:58:22 +00:00
|
|
|
|
2015-03-28 21:51:50 +00: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-23 04:58:22 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-10-18 22:56:52 +00:00
|
|
|
var odds = (1..6).where {|n| n % 2 == 1 }
|
2015-04-01 14:31:15 +00:00
|
|
|
for (n in odds) {
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print(n) //> 1
|
|
|
|
|
//> 3
|
|
|
|
|
//> 5
|
2015-04-01 14:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2015-04-06 15:21:32 +00:00
|
|
|
To force eager evaluation, just call `.toList` on the result.
|
2015-03-28 21:51:50 +00:00
|
|
|
|
2015-09-22 14:59:54 +00:00
|
|
|
:::wren
|
2015-04-01 14:31:15 +00:00
|
|
|
var numbers = [1, 2, 3, 4, 5, 6]
|
2015-04-06 15:21:32 +00:00
|
|
|
var odds = numbers.where {|n| n % 2 == 1 }.toList
|
2015-04-01 14:31:15 +00:00
|
|
|
numbers.add(7)
|
2015-10-18 22:56:52 +00:00
|
|
|
System.print(odds) //> [1, 3, 5]
|