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
+6
View File
@@ -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(_)'.