tc39 / proposal-math-sum

TC39 proposal to add a summation method to JavaScript
https://tc39.github.io/proposal-math-sum
76 stars 2 forks source link

early exit when state becomes not-a-number #11

Closed michaelficarra closed 5 months ago

michaelficarra commented 6 months ago

The current spec text has a state (not-a-number) which can be entered but cannot be left. Why not early exit when we would enter this state? I don't see a good reason to consume the whole iterator. I don't care to have it throw if my iterator would have produced a non-Number if that's what you were going for.

bakkot commented 6 months ago

For consistency with every other method on Math, mostly. It seems weird to stop halfway.

bakkot commented 6 months ago

I also don't like that Math.sumExact(["foo", NaN]) would throw whereas Math.sumExact([NaN, "foo"]) wouldn't.

michaelficarra commented 6 months ago

Yeah I don't weigh those as highly as getting a quick exit when the first Number in my billion-Number-yielding iterator is NaN.

bakkot commented 6 months ago

Why are there any NaNs in your billion-Number-yielding iterator? That doesn't seem like a case we should be optimizing for.

michaelficarra commented 6 months ago

For any reason that NaNs show up generally. Could be from certain arithmetic operations. Could be from parsing unconstrained inputs. There's lots of reasons.

bakkot commented 5 months ago

In plenary some were in favor and some were against this change, but the balance of opinion was against.

michaelficarra commented 4 months ago

Assuming we get Iterator.prototype.takeWhile, I guess you can just stick a .takeWhile(n => !Number.isNaN(n)) onto your iterator. Unfortunately, that also puts the responsibility of pulling an iterable iterator out of the iterator on the consumer.