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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user