feat: add Sequence.count(predicate) method and update documentation

Implement a new `count(f)` method on the Sequence class that accepts a predicate function and returns the number of elements for which the predicate evaluates to true. The implementation iterates over the sequence, calling `f.call(element)` on each element and incrementing a counter when the result is truthy.

Also update the documentation for both Sequence and List classes: add full documentation for the new `count(predicate)` method on Sequence, and change the terminology in List docs from "items" to "elements" for consistency. Add three test files covering the basic predicate behavior, non-boolean return values from the predicate, and the error case when a non-function argument is passed.
This commit is contained in:
Thorbjørn Lindeijer
2015-03-14 13:17:21 +00:00
parent 398a693d3c
commit 9dc939380c
7 changed files with 52 additions and 2 deletions
+10
View File
@@ -16,6 +16,16 @@ class Sequence {
return result
}
count(f) {
var result = 0
for (element in this) {
if (f.call(element)) {
result = result + 1
}
}
return result
}
map(f) {
var result = new List
for (element in this) {