Closed Bug 305002 Opened 19 years ago Closed 19 years ago

Array.every should return |true| on empty arrays

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: engel, Assigned: engel)

References

Details

(Keywords: fixed1.8, js1.5)

Attachments

(1 file, 1 obsolete file)

The behavior of |every| is described in
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:every#Description
  |every| executes the provided function (callback) once for each element 
  present in the array until it finds one where callback returns a false value. 
  If such an element is found, the test aborts and |false| is returned, 
  otherwise (callback returned a true value for each of the elements) |every| 
  will return |true|. 

Thus, for an empty array |a = []|,  |a.every(f)| must always return |true|
(because no element is found where the callback returns |false|, in fact the
callback is never called at all).


This definition for |every| makes much sense, since for an array |a| and a
constant callback |f|, |every| and |some| are related via
   a.every(f) == !a.some(not_f),
which always evaluates to true with 
   function not_f(x) { return !f(x); }

This relation holds for an empty array if |every| returns |true| and |some|
returns |false|.
Historical note: The current behavior of returning |false| on empty arrays was
introduced in Bug 290592, Comment 22.
Attached patch [ ].every(f) now returns true (obsolete) — Splinter Review
Attachment #192977 - Flags: review?(shaver)
Comment on attachment 192977 [details] [diff] [review]
[ ].every(f)  now returns true

r=shaver, thanks.
Attachment #192977 - Flags: review?(shaver) → review+
With this fix, could you remove the |else if (mode == EVERY)| clause just before
the out label?
Thank you for the hint.  Indeed, now the assigments to |*rval| after the loop
over the array can be removed (both for |some| and for |every|).
Attachment #192977 - Attachment is obsolete: true
Attachment #193100 - Flags: superreview?(brendan)
Attachment #193100 - Flags: review?(shaver)
Comment on attachment 193100 [details] [diff] [review]
[ ].every(f) now returns true; also removing extra assignments at end of loop

sr+a=me -- mrbkap, can you do the honors for trunk and 1.8 branch?  Thanks,

/be
Attachment #193100 - Flags: superreview?(brendan)
Attachment #193100 - Flags: superreview+
Attachment #193100 - Flags: review?(shaver)
Attachment #193100 - Flags: review+
Attachment #193100 - Flags: approval1.8b4+
Checked in.  Thanks for the patch, Hans-Andreas!

/be
Status: NEW → RESOLVED
Closed: 19 years ago
Resolution: --- → FIXED
Flags: blocking1.8b4+
Keywords: fixed1.8
Checking in regress-305002.js;
/cvsroot/mozilla/js/tests/js1_6/Array/regress-305002.js,v  <--  regress-305002.js
initial revision: 1.1
done
Flags: testcase+
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: