The default bug view has changed. See this FAQ.

Array.every should return |true| on empty arrays

RESOLVED FIXED

Status

()

Core
JavaScript Engine
RESOLVED FIXED
12 years ago
11 years ago

People

(Reporter: Hans-Andreas Engel, Assigned: Hans-Andreas Engel)

Tracking

({fixed1.8, js1.5})

Trunk
fixed1.8, js1.5
Points:
---
Bug Flags:
blocking1.8b5 +
in-testsuite +

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment, 1 obsolete attachment)

(Assignee)

Description

12 years ago
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|.
(Assignee)

Comment 1

12 years ago
Historical note: The current behavior of returning |false| on empty arrays was
introduced in Bug 290592, Comment 22.
(Assignee)

Comment 2

12 years ago
Created attachment 192977 [details] [diff] [review]
[ ].every(f)  now returns true
(Assignee)

Updated

12 years ago
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?
(Assignee)

Comment 5

12 years ago
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|).
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
Last Resolved: 12 years ago
Resolution: --- → FIXED

Updated

12 years ago
Flags: blocking1.8b4+
Keywords: fixed1.8

Comment 8

12 years ago
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.