feat: make Sequence.all() and Sequence.any() return actual predicate values instead of booleans

Rewrite all() and any() to store and return the predicate's result directly, matching && and || semantics. Update documentation to describe the new behavior. Move tests from test/core/list/ to test/core/sequence/ and add cases verifying that the first falsey/truthy value is returned.
This commit is contained in:
Bob Nystrom
2015-03-28 17:18:45 +00:00
parent adc64d65b9
commit 540a4a166a
11 changed files with 64 additions and 65 deletions
+20 -20
View File
@@ -1,9 +1,25 @@
class Sequence {
all(f) {
var result = true
for (element in this) {
result = f.call(element)
if (!result) return result
}
return result
}
any(f) {
var result = false
for (element in this) {
result = f.call(element)
if (result) return result
}
return result
}
contains(element) {
for (item in this) {
if (element == item) {
return true
}
if (element == item) return true
}
return false
}
@@ -19,9 +35,7 @@ class Sequence {
count(f) {
var result = 0
for (element in this) {
if (f.call(element)) {
result = result + 1
}
if (f.call(element)) result = result + 1
}
return result
}
@@ -42,20 +56,6 @@ class Sequence {
return result
}
all(f) {
for (element in this) {
if (!f.call(element)) return false
}
return true
}
any(f) {
for (element in this) {
if (f.call(element)) return true
}
return false
}
reduce(acc, f) {
for (element in this) {
acc = f.call(acc, element)