3 ms·
Isn't the loop let max = list[0]; for (let i = 0; i < list.length; i++) { max = Math.max(list[i], max); } at the end just implementing the va
by JadeNB 2y ago
Isn't the loop
let max = list[0];
for (let i = 0; i < list.length; i++) {
max = Math.max(list[i], max);
}
at the end just implementing the variadic `Math.max()` of JavaScript by pretending that it's binary? (I bring up JavaScript because the text seems to indicate that's the language in which the snippets are written: "You suggest an alternative, wincing slightly at the lambda notation you need to avoid running afoul of JavaScript’s variadic Math.max(): ….")
- geocar 2y ago> Isn't the loop ... at the end just implementing the variadic `Math.max()` of JavaScript by pretending that it's binary? I assume you're talking about: Math.max.apply(Math,list) This isn't exactly the same if list=[] (the loop yields undefined; Math.max.apply returns -Infinity), so maybe this is what he is referring to. This variadic capability is also really specific to Math.max (and min, and a few other functions), and wasn't in early JavaScript so maybe John just doesn't know. But the main point I think is that it's much much longer than this: |/list
- RodgerTheGreat 2y agoThere's also "spread application" syntax in JS, like `Math.max(...list)`, but for many years browsers might permit it for small lists while crashing when supplied with large lists; the compatibility tradeoffs and non-obvious behaviors of all these alternatives make the point pretty clearly, I think.
- geocar 2y agoHi John. Big lists are a good point. I didn't think of that. Cheers. function ta() { window.la = arguments.length; } for (var i = 1; i < 32; i++) { var list = new Array(1<<i); ta(...list) } la is only 65536 in Safari and Chrome; 262144 on Firefox. I have no idea what MSIE does. Using ta.apply(null,list) gives same results.