Math.minmax

Michael Rosefield rosyatrandom at gmail.com
Mon Oct 2 15:47:48 UTC 2017


Oh, and of course if I'm going to be that guy I should immediately post a
slightly better version just to annoy people more:

```js
const minMax = (arr=[]) => arr.reduce(
   ([ min=Infinity, max=-Infinity ], curr) => [ Math.min(curr, min),
Math.max(curr, max) ], []
  )

On Mon, 2 Oct 2017 at 16:44 Michael Rosefield <rosyatrandom at gmail.com>
wrote:

> I think your approach is fine, but just to be that guy I'll condense it
> some more (could be output as a hash but, if we're going to condense,
> well...):
>
> ```js
> const minMax = arr => arr.reduce(
>    ([ min, max ], curr) => [ Math.min(curr, min), Math.max(curr, max) ],
>    [ Infinity, -Infinity ]
>   )
>
> On Mon, 2 Oct 2017 at 16:34 Naveen Chawla <naveen.chwl at gmail.com> wrote:
>
>> I would just use reduce for this. Reason: I think any multi var result
>> format is a little messy. I find it better to let the dev decide on the
>> result format, e.g.:
>>
>> ```js
>> const minMax =
>>      array.reduce(
>>          (accumulator, currentValue)=>{
>>               return {
>>                   min: Math.min(currentValue, accumulator.min),
>>                   max: Math.max(currentValue, accumulator.max)
>>               }
>>          },
>>          {
>>              min: Infinity,
>>              max: -Infinity
>>          }
>>      )
>> ```
>>
>> Good thing is, this can easily be refactored to accept arrays with
>> objects that contain the values, instead of just an array of numbers, as
>> well as the ability to calculator other accumulated values (e.g. mean
>> average etc.) in the same call.
>>
>> Do let me know if you think I'm missing the point
>>
>> On Mon, 2 Oct 2017 at 19:16 Boris Zbarsky <bzbarsky at mit.edu> wrote:
>>
>>> On 10/2/17 7:10 AM, Xavier Stouder wrote:
>>> > Don't know what Boris mean when he talks about recreation bits
>>>
>>> Fwiw, it looks like the code at
>>> https://esbench.com/bench/595c1b1899634800a03488b9 does not have the
>>> array recreation bits (function whatever(...args)) that earlier
>>> benchmarks for this had.
>>>
>>> -Boris
>>> _______________________________________________
>>> 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/20171002/a5c9a0b1/attachment-0001.html>


More information about the es-discuss mailing list