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
every
to test if all elements in an array pass some test.
- You use
some
to test if at least one element in an array passes some
test.
- You use
map
to 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
filter
to return an array of length 0 < length
< initial array length
elements, all contained in the original array and all
passing the supplied predicate test.
- You use
foreach
if you want map but in-place
- You use
reduce
if 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!
some
, I want to do something with the element, if I useevery
, I want to do something to all of those elements...some
andevery
don't let me access that information, so either I can't use them, or I have to add side-effects.some
in myif
condition to determine whether a certain element in the array exhibits a certain property, 9/10 I need to operate on that element in myif
body; now, assome
doesn't tell me which of the elements exhibited the property (just "one did"), I can either usesome
again in the body (O(2n)), or I can just perform the operation inside the if condition (which is bad, because its a side-effect within the head).every
as well, of course.