Array.prototype.max and Array.prototype.min
Why add new api surface when this works as is:
Math.max.apply( null, [ 1, 2, 3, 4 ] ); // 4
Math.min.apply( null, [ 1, 2, 3, 4 ] ); // 1
Why add new api surface when this works as is: Math.max.apply( null, [ 1, 2, 3, 4 ] ); // 4 Math.min.apply( null, [ 1, 2, 3, 4 ] ); // 1 On Mon, Feb 20, 2012 at 2:25 PM, Xavier MONTILLET <xavierm02.net at gmail.com>wrote: > Hi, > > I think it'd be nice to have Array.prototype.max and Array.prototype.min. > > What they would do is this: > > Array.prototype.max = function ( f ) { > return this.sort( f )[ this.length - 1 ]; > }; > Array.prototype.min = function ( f ) { > return this.sort( f )[ 0 ]; > }; > > Of course, that's not how they would actually be implemented (sorting > the whole array to get the max/minimum is ridiculous) but I wrote them > that way so that we clearly see the behavior when a function is given > as argument: just as with Array.prototype.sort, the function is used > instead of < in the comparisons. > > Here is a more efficient implementation: > > Array.prototype.max = function ( f ) { > var max = this[ 0 ]; > var i; > var l = this.length; > var item; > if ( f ) { > for ( i = 1; i < l; ++i ) { > item = this[ i ]; > if ( item > max ) { > max = item; > } > } > } else { > for ( i = 1; i < l; ++i ) { > item = this[ i ]; > if ( f( item, max ) === 1 ) {// maybe it should be -1... I > never remember... > max = item; > } > } > } > return max; > }; > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120220/099121eb/attachment.html>
Because I'm not talking of arrays containing only numbers.
Because I'm not talking of arrays containing only numbers. On Mon, Feb 20, 2012 at 8:29 PM, Rick Waldron <waldron.rick at gmail.com> wrote: > Why add new api surface when this works as is: > > Math.max.apply( null, [ 1, 2, 3, 4 ] ); > // 4 > > > Math.min.apply( null, [ 1, 2, 3, 4 ] ); > // 1 > > > > On Mon, Feb 20, 2012 at 2:25 PM, Xavier MONTILLET <xavierm02.net at gmail.com> > wrote: >> >> Hi, >> >> I think it'd be nice to have Array.prototype.max and Array.prototype.min. >> >> What they would do is this: >> >> Array.prototype.max = function ( f ) { >> return this.sort( f )[ this.length - 1 ]; >> }; >> Array.prototype.min = function ( f ) { >> return this.sort( f )[ 0 ]; >> }; >> >> Of course, that's not how they would actually be implemented (sorting >> the whole array to get the max/minimum is ridiculous) but I wrote them >> that way so that we clearly see the behavior when a function is given >> as argument: just as with Array.prototype.sort, the function is used >> instead of < in the comparisons. >> >> Here is a more efficient implementation: >> >> Array.prototype.max = function ( f ) { >> var max = this[ 0 ]; >> var i; >> var l = this.length; >> var item; >> if ( f ) { >> for ( i = 1; i < l; ++i ) { >> item = this[ i ]; >> if ( item > max ) { >> max = item; >> } >> } >> } else { >> for ( i = 1; i < l; ++i ) { >> item = this[ i ]; >> if ( f( item, max ) === 1 ) {// maybe it should be -1... I >> never remember... >> max = item; >> } >> } >> } >> return max; >> }; >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss > >
In that case, you should not even compare tomatoes and potatoes, a side effect you may have when greater or "less than" is used in your example or sort, without any callback to invoke able to compare, could produce: unexpected results.
"a" and "0a" ... who's first here ? [0] and {length:0}, who is max who is min?
etc
In that case, you should not even compare tomatoes and potatoes, a side effect you may have when greater or "less than" is used in your example or sort, without any callback to invoke able to compare, could produce: unexpected results. "a" and "0a" ... who's first here ? [0] and {length:0}, who is max who is min? etc On Mon, Feb 20, 2012 at 8:32 PM, Xavier MONTILLET <xavierm02.net at gmail.com>wrote: > Because I'm not talking of arrays containing only numbers. > > On Mon, Feb 20, 2012 at 8:29 PM, Rick Waldron <waldron.rick at gmail.com> > wrote: > > Why add new api surface when this works as is: > > > > Math.max.apply( null, [ 1, 2, 3, 4 ] ); > > // 4 > > > > > > Math.min.apply( null, [ 1, 2, 3, 4 ] ); > > // 1 > > > > > > > > On Mon, Feb 20, 2012 at 2:25 PM, Xavier MONTILLET < > xavierm02.net at gmail.com> > > wrote: > >> > >> Hi, > >> > >> I think it'd be nice to have Array.prototype.max and > Array.prototype.min. > >> > >> What they would do is this: > >> > >> Array.prototype.max = function ( f ) { > >> return this.sort( f )[ this.length - 1 ]; > >> }; > >> Array.prototype.min = function ( f ) { > >> return this.sort( f )[ 0 ]; > >> }; > >> > >> Of course, that's not how they would actually be implemented (sorting > >> the whole array to get the max/minimum is ridiculous) but I wrote them > >> that way so that we clearly see the behavior when a function is given > >> as argument: just as with Array.prototype.sort, the function is used > >> instead of < in the comparisons. > >> > >> Here is a more efficient implementation: > >> > >> Array.prototype.max = function ( f ) { > >> var max = this[ 0 ]; > >> var i; > >> var l = this.length; > >> var item; > >> if ( f ) { > >> for ( i = 1; i < l; ++i ) { > >> item = this[ i ]; > >> if ( item > max ) { > >> max = item; > >> } > >> } > >> } else { > >> for ( i = 1; i < l; ++i ) { > >> item = this[ i ]; > >> if ( f( item, max ) === 1 ) {// maybe it should be -1... I > >> never remember... > >> max = item; > >> } > >> } > >> } > >> return max; > >> }; > >> _______________________________________________ > >> es-discuss mailing list > >> es-discuss at mozilla.org > >> https://mail.mozilla.org/listinfo/es-discuss > > > > > _______________________________________________ > es-discuss mailing list > es-discuss at mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120220/b1f0cf26/attachment.html>
I mistyped. I wrote "if ( f ) {" instead of "if ( ! f ) {" So when I compare objects with a function, I'm in the "else" part and I check "if f( item, max ) == 1" instead of "if item < max".
Array.prototype.max = function ( f ) { var max = this[ 0 ]; var i; var l = this.length; var item; if ( ! f ) {// @@@ I only changed this line @@@ for ( i = 1; i < l; ++i ) { item = this[ i ]; if ( item > max ) { max = item; } } } else { for ( i = 1; i < l; ++i ) { item = this[ i ]; if ( f( item, max ) === 1 ) {// maybe it should be -1... I never remember... max = item; } } } return max; };
Here's a simple example:
var array = [ { v: 1 }, { v: 3 }, { v: 2 } ]; array.max( function ( a, b ) { return a.v - b.v; } );
I mistyped. I wrote "if ( f ) {" instead of "if ( ! f ) {" So when I compare objects with a function, I'm in the "else" part and I check "if f( item, max ) == 1" instead of "if item < max". Array.prototype.max = function ( f ) { var max = this[ 0 ]; var i; var l = this.length; var item; if ( ! f ) {// @@@ I only changed this line @@@ for ( i = 1; i < l; ++i ) { item = this[ i ]; if ( item > max ) { max = item; } } } else { for ( i = 1; i < l; ++i ) { item = this[ i ]; if ( f( item, max ) === 1 ) {// maybe it should be -1... I never remember... max = item; } } } return max; }; Here's a simple example: var array = [ { v: 1 }, { v: 3 }, { v: 2 } ]; array.max( function ( a, b ) { return a.v - b.v; } ); On Mon, Feb 20, 2012 at 9:02 PM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: > In that case, you should not even compare tomatoes and potatoes, a side > effect you may have when greater or "less than" is used in your example or > sort, without any callback to invoke able to compare, could produce: > unexpected results. > > "a" and "0a" ... who's first here ? > [0] and {length:0}, who is max who is min? > > etc > > > On Mon, Feb 20, 2012 at 8:32 PM, Xavier MONTILLET <xavierm02.net at gmail.com> > wrote: >> >> Because I'm not talking of arrays containing only numbers. >> >> On Mon, Feb 20, 2012 at 8:29 PM, Rick Waldron <waldron.rick at gmail.com> >> wrote: >> > Why add new api surface when this works as is: >> > >> > Math.max.apply( null, [ 1, 2, 3, 4 ] ); >> > // 4 >> > >> > >> > Math.min.apply( null, [ 1, 2, 3, 4 ] ); >> > // 1 >> > >> > >> > >> > On Mon, Feb 20, 2012 at 2:25 PM, Xavier MONTILLET >> > <xavierm02.net at gmail.com> >> > wrote: >> >> >> >> Hi, >> >> >> >> I think it'd be nice to have Array.prototype.max and >> >> Array.prototype.min. >> >> >> >> What they would do is this: >> >> >> >> Array.prototype.max = function ( f ) { >> >> return this.sort( f )[ this.length - 1 ]; >> >> }; >> >> Array.prototype.min = function ( f ) { >> >> return this.sort( f )[ 0 ]; >> >> }; >> >> >> >> Of course, that's not how they would actually be implemented (sorting >> >> the whole array to get the max/minimum is ridiculous) but I wrote them >> >> that way so that we clearly see the behavior when a function is given >> >> as argument: just as with Array.prototype.sort, the function is used >> >> instead of < in the comparisons. >> >> >> >> Here is a more efficient implementation: >> >> >> >> Array.prototype.max = function ( f ) { >> >> var max = this[ 0 ]; >> >> var i; >> >> var l = this.length; >> >> var item; >> >> if ( f ) { >> >> for ( i = 1; i < l; ++i ) { >> >> item = this[ i ]; >> >> if ( item > max ) { >> >> max = item; >> >> } >> >> } >> >> } else { >> >> for ( i = 1; i < l; ++i ) { >> >> item = this[ i ]; >> >> if ( f( item, max ) === 1 ) {// maybe it should be -1... I >> >> never remember... >> >> max = item; >> >> } >> >> } >> >> } >> >> return max; >> >> }; >> >> _______________________________________________ >> >> es-discuss mailing list >> >> es-discuss at mozilla.org >> >> https://mail.mozilla.org/listinfo/es-discuss >> > >> > >> _______________________________________________ >> es-discuss mailing list >> es-discuss at mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss > >
I think it'd be nice to have Array.prototype.max and Array.prototype.min.
What they would do is this:
Array.prototype.max = function ( f ) { return this.sort( f )[ this.length - 1 ]; }; Array.prototype.min = function ( f ) { return this.sort( f )[ 0 ]; };
Of course, that's not how they would actually be implemented (sorting the whole array to get the max/minimum is ridiculous) but I wrote them that way so that we clearly see the behavior when a function is given as argument: just as with Array.prototype.sort, the function is used instead of < in the comparisons.
Here is a more efficient implementation:
Array.prototype.max = function ( f ) { var max = this[ 0 ]; var i; var l = this.length; var item; if ( f ) { for ( i = 1; i < l; ++i ) { item = this[ i ]; if ( item > max ) { max = item; } } } else { for ( i = 1; i < l; ++i ) { item = this[ i ]; if ( f( item, max ) === 1 ) {// maybe it should be -1... I never remember... max = item; } } } return max; };