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
+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) {