Define .forEach
, .some
, and .every
methods
#14
Labels
Change: minor
[Issue / PR] describes a non-breaking change, such as adding a new functionality
Domain: main
[Issue / PR] describes change in the functionality, its optimization
good first issue
[Issue] can be addressed by a first-time contributor
Pending: blocked
[Issue / PR] cannot be addressed until another issue is resolved
Priority: low
[Issue / PR] could be addressed at any convenient time
Type: improvement
[Issue / PR] addresses lack of a functionality or an open possibility of enhancement
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:
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 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
?) ↑The text was updated successfully, but these errors were encountered: