As the name suggests, an Array
that guarantees the invariant of length > 0
and has no empty slots (never sparse). This ensures length
always matches the actual count of elements, reduces "foot-guns", and it "matches" the name of the class
itself! (the latter point is just an aesthetic reason)
There was a previous post explaining why a new kind of Array
isn't a great idea, but (for this case) I believe it makes sense for it to be distinct, as the behavior of multiple methods must be changed (the following lists are non-exhaustive):
pop
&shift
mustthrow
instead of returningundefined
reduce
doesn't take an initial param, and is guaranteed to onlythrow
what the callbackthrow
ssplice
Other methods would return new NonEmptyArray
instances (or a ref to the same N.E.A.) rather than Array
s:
concat
fill
map
toReversed
&reverse
toSorted
&sort
Others are yet-to-be defined (throw
? always Array
? conditionally return Array
and N.E.A.?):
slice
filter
toSpliced