(If you know C# LINQ , it's like Any vs All)
somewill return true if any predicate istrueeverywill return true if all predicate istrue
Where predicate means function that returns bool ( true/false) for each element
every returns on first false.
some returns on first true
What is the correct situation to use array.find() vs. array.includes vs. array.some() ?
Enumerable#any vs Array#some
(If you know C# LINQ , it's like Any vs All)
somewill return true if any predicate istrueeverywill return true if all predicate istrue
Where predicate means function that returns bool ( true/false) for each element
every returns on first false.
some returns on first true
some is analogue to logical or
every is analogue to logical and
logically every implies some, but not in reverse
try this:
var identity = function(x){return x}
console.log([true, true].some(identity))//true
console.log([true, true].every(identity))//true
console.log([true, false].some(identity))//true
console.log([true, false].every(identity))//false
console.log([false, false].some(identity))//false
console.log([false, false].every(identity))//false
console.log([undefined, true].some(identity))//true
console.log([undefined, true].every(identity))//false
console.log([undefined, false].some(identity))//false
console.log([undefined, false].every(identity))//false
console.log([undefined, undefined].some(identity))//false
console.log([undefined, undefined].every(identity))//false
I understand how each of these methods work, but they all seem to have similar functions to one another, and I'm not sure as to what situation might call for what method.
If I understand your point correctly, you seem to be mis-using or abusing every and some but it's a little unavoidable if you want to change the elements of your arrays directly. Correct me if I'm wrong, but what you're trying to do is find out if some or every element in your sequence exhibits a certain condition then modify those elements. Also, your code seems to be applying something to all items until you find one that doesn't pass the predicate and I don't think that's what you mean to be doing. Anyways.
Let's take your first example (slightly modified)
if (input.every(function (that) {
return typeof that === "number";
})) {
input.every(function (that) {
that.foo();
}
} else {
return;
}
What you're doing here actually goes a little against the spirit of the some/every/map/reduce/filter/etc concepts. Every isn't meant to be used to affect every item that conforms to something, rather it should only be used to tell you if every item in a collection does. If you want to apply a function to all items for which a predicate evaluates to true, the "good" way to do it is
var filtered = array.filter(function(item) {
return typeof item === "number";
});
var mapped = filtered.map(function(item) {
return item.foo(); //provided foo() has no side effects and returns a new object of item's type instead. See note about foreach below.
});
Alternatively, you could use foreach instead of map to modify the items in-place.
The same logic applies to some, basically:
- You use
everyto test if all elements in an array pass some test. - You use
someto test if at least one element in an array passes some test. - You use
mapto return a new array containing 1 element (which is the result of a function of your choice) for every element in an input array. - You use
filterto return an array of length 0 <length<initial array lengthelements, all contained in the original array and all passing the supplied predicate test. - You use
foreachif you want map but in-place - You use
reduceif you want to combine the results of an array in a single object result (which could be an array but doesn't have to).
The more you use them (and the more you write LISP code), the more you realize how they are related and how it's even possible to emulate/implement one with the others. What's powerful with these queries and what's really interesting is their semantics, and how they really push you towards eliminating harmful side-effects in your code.
EDIT (in light of comments): So let's say you want to validate that every element is an object and convert them to an Application Model if they're all valid. One way to do this in a single pass would be:
var dirty = false;
var app_domain_objects = input.map(function(item) {
if(validate(item)) {
return new Model(item);
} else {
dirty = true; //dirty is captured by the function passed to map, but you know that :)
}
});
if(dirty) {
//your validation test failed, do w/e you need to
} else {
//You can use app_domain_objects
}
This way, when an object doesn't pass validation, you still keep iterating through the entire array, which would be slower than just validating with every. However, most of the time your array will be valid (or I should hope so), so in most cases you'll perform a single pass over your array and end up with a usable array of Application Model objects. Semantics will be respected, side-effects avoided and everyone will be happy!
Note that you could also write your own query, similar to foreach, which would apply a function to all members of an array and returns true/false if they all pass a predicate test. Something like:
function apply_to_every(arr, predicate, func) {
var passed = true;
for(var i = 0; i < array.length; ++i) {
if(predicate(arr[i])) {
func(arr[i]);
} else {
passed = false;
break;
}
}
return passed;
}
Although that would modify the array in place.
I hope this helps, it was very fun to write. Cheers!
The side effects are not in the if condition, they are in the if's body. You have only determined whether or not to execute that body in the actual condition. There is nothing wrong with your approach here.