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:
@@ -0,0 +1,6 @@
|
||||
var a = [1, 2, 3]
|
||||
|
||||
IO.print(a.count {|x| x > 3 }) // expect: 0
|
||||
IO.print(a.count {|x| x > 1 }) // expect: 2
|
||||
|
||||
IO.print([].count {|x| true }) // expect: 0
|
||||
@@ -0,0 +1,3 @@
|
||||
var a = [1, 2, 3]
|
||||
|
||||
IO.print(a.count {|x| "truthy" }) // expect: 3
|
||||
@@ -0,0 +1,3 @@
|
||||
var a = [1, 2, 3]
|
||||
|
||||
a.count("string") // expect runtime error: String does not implement 'call(_)'.
|
||||
Reference in New Issue
Block a user