Added Sequence.count(predicate)

Returns the number of elements in the sequence that pass the
`predicate`.

It could also have been implemented as:

  count(f) { reduce(0) {|a, b| f.call(b) ? a + 1 : a } }

But I considered the simple version more readable.

Also documented Sequence.count.
This commit is contained in:
Thorbjørn Lindeijer
2015-03-19 21:10:05 +01:00
parent 5e1ddb7db5
commit 94804fe1a0
7 changed files with 52 additions and 2 deletions
+18
View File
@@ -34,6 +34,24 @@ Otherwise, returns `false`.
Returns whether the sequence contains any element equal to the given element.
### **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.
### **join**(sep)
Returns a string representation of the list. The string representations of the