Last Comment Bug 305002 - Array.every should return |true| on empty arrays
: Array.every should return |true| on empty arrays
Status: RESOLVED FIXED
: fixed1.8, js1.5
Product: Core
Classification: Components
Component: JavaScript Engine (show other bugs)
: Trunk
: All All
: -- normal (vote)
: ---
Assigned To: Hans-Andreas Engel
:
Mentors:
Depends on: 290592
Blocks:
  Show dependency treegraph
 
Reported: 2005-08-17 12:03 PDT by Hans-Andreas Engel
Modified: 2006-03-12 18:49 PST (History)
5 users (show)
brendan: blocking1.8b5+
bob: in‑testsuite+
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---


Attachments
[ ].every(f) now returns true (837 bytes, patch)
2005-08-17 12:12 PDT, Hans-Andreas Engel
shaver: review+
Details | Diff | Review
[ ].every(f) now returns true; also removing extra assignments at end of loop (1.29 KB, patch)
2005-08-18 15:41 PDT, Hans-Andreas Engel
brendan: review+
brendan: superreview+
brendan: approval1.8b4+
Details | Diff | Review

Description Hans-Andreas Engel 2005-08-17 12:03:33 PDT
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|.
Comment 1 Hans-Andreas Engel 2005-08-17 12:04:58 PDT
Historical note: The current behavior of returning |false| on empty arrays was
introduced in Bug 290592, Comment 22.
Comment 2 Hans-Andreas Engel 2005-08-17 12:12:46 PDT
Created attachment 192977 [details] [diff] [review]
[ ].every(f)  now returns true
Comment 3 Mike Shaver (:shaver -- probably not reading bugmail closely) 2005-08-18 10:13:53 PDT
Comment on attachment 192977 [details] [diff] [review]
[ ].every(f)  now returns true

r=shaver, thanks.
Comment 4 Blake Kaplan (:mrbkap) (please use needinfo!) 2005-08-18 10:19:12 PDT
With this fix, could you remove the |else if (mode == EVERY)| clause just before
the out label?
Comment 5 Hans-Andreas Engel 2005-08-18 15:41:58 PDT
Created attachment 193100 [details] [diff] [review]
[ ].every(f) now returns true; also removing extra assignments at end of loop

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|).
Comment 6 Brendan Eich [:brendan] 2005-08-18 15:48:49 PDT
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
Comment 7 Brendan Eich [:brendan] 2005-08-18 18:51:41 PDT
Checked in.  Thanks for the patch, Hans-Andreas!

/be
Comment 8 Bob Clary [:bc:] 2005-10-23 05:49:30 PDT
Checking in regress-305002.js;
/cvsroot/mozilla/js/tests/js1_6/Array/regress-305002.js,v  <--  regress-305002.js
initial revision: 1.1
done

Note You need to log in before you can comment on or make changes to this bug.