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:
+20
-20
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user