Tweak Sequence.all() and Sequence.any().

When possible, they return the actual value from the predicate
instead of always just "true" and "false". This matches && and ||
which evaluate to the RHS or LHS when appropriate.
This commit is contained in:
Bob Nystrom
2015-03-28 10:18:45 -07:00
parent a7fafce265
commit 07f9d4d2be
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)