Fires after each of the iteration; there's no way to manually "stop" it, — it stops either automatically with the end of the range (which means it shouldn't be used with infinite ranges), or when its callback throws. Doesn't fire for non-iterated (read "empty") ranges.
.some
Short-circuited cousin of .forEach. Returns whether at least one of the calculated items fits the logic of predicate; consequently, returns false for non-iterated (read "empty") ranges. For infinite ranges either returns true (when the first fitting item is found), or hangs forever.
.every
Short-circuited cousin of .forEach. Returns whether strictly all of the calculated items fit the logic of predicate; surprisingly, returns true for non-iterated (read "empty") ranges, because of the concept of vacuous truth. For infinite ranges either returns false (when the first unfitting item is found), or hangs forever.
Considerations:
all three methods are provided with the most recently calculated item — curr, number of previously calculated items — count, and (optionally) the list of previously calculated items themselves — memo;
since count is the number of previously calculated items, it is 0-based, – i.e., by the time of the nth iteration, there are n - 1 previously calculated items;
memo is the array of previously calculated items in reverse order (i.e., latest to earliest), and curr is the most recent of them: curr === memo[0];
// in all of these scenarios, `memo` is empty,
// since value of `callback.length` is less than 3
range.forEach(() => {});
range.forEach((curr) => {});
range.forEach((curr, count) => {});
range.forEach((curr, count, memo = []) => {});
range.forEach((curr, count = NaN, memo) => {});
range.forEach((curr = null, count, memo) => {});
// these callbacks have `.length` equal to or greater than 3,
// which allows `memo` to be calculated and provided
range.forEach((curr, count, memo) => {});
range.forEach((curr, count, memo, arg3) => {});
range.forEach((curr, count, memo, arg3, arg4) => {});
range.forEach((curr, count, memo, arg3 = DEFAULT_VALUE, arg4) => {});
range.forEach((curr, count, memo, ...args) => {});
Having more than three parameters however is useless here, since at most there will be only three arguments provided to callback, the rest of parameters will always remain undefined (just like in day-to-day JavaScript).
1 – For other methods (like .map of .filter etc.) require using Array.from, or perhaps consider creating separate npm package (@xrange/extras?) ↑
Define analogs of
.forEach
,.some
, and.every
methods ofArray.prototype
1..forEach
Fires after each of the iteration; there's no way to manually "stop" it, — it stops either automatically with the end of the range (which means it shouldn't be used with infinite ranges), or when its callback
throw
s. Doesn't fire for non-iterated (read "empty") ranges..some
Short-circuited cousin of
.forEach
. Returns whether at least one of the calculated items fits the logic ofpredicate
; consequently, returnsfalse
for non-iterated (read "empty") ranges. For infinite ranges either returnstrue
(when the first fitting item is found), or hangs forever..every
Short-circuited cousin of
.forEach
. Returns whether strictly all of the calculated items fit the logic ofpredicate
; surprisingly, returnstrue
for non-iterated (read "empty") ranges, because of the concept of vacuous truth. For infinite ranges either returnsfalse
(when the first unfitting item is found), or hangs forever.Considerations:
curr
, number of previously calculated items —count
, and (optionally) the list of previously calculated items themselves —memo
;count
is the number of previously calculated items, it is0
-based, – i.e., by the time of the nth iteration, there aren - 1
previously calculated items;memo
is the array of previously calculated items in reverse order (i.e., latest to earliest), andcurr
is the most recent of them:curr === memo[0]
;depending on the value of
predicate.length
(wherepredicate
is a function), the value ofmemo
might be an empty array, regardless of the actual iterations (for optimization reasons):Having more than three parameters however is useless here, since at most there will be only three arguments provided to callback, the rest of parameters will always remain
undefined
(just like in day-to-day JavaScript).1 – For other methods (like
.map
of.filter
etc.) require usingArray.from
, or perhaps consider creating separate npm package (@xrange/extras
?) ↑