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
@@ -62,6 +62,16 @@ static const char* libSource =
" return result\n"
" }\n"
"\n"
" count(f) {\n"
" var result = 0\n"
" for (element in this) {\n"
" if (f.call(element)) {\n"
" result = result + 1\n"
" }\n"
" }\n"
" return result\n"
" }\n"
"\n"
" map(f) {\n"
" var result = new List\n"
" for (element in this) {\n"