Closed ByteEater-pl closed 5 years ago
I personally think we should careful about adding things on the prototype.
number#to
but I don't want to see 0 .to
, 0..to
, or (0).to
any more. So I would prefer vote -1 here.Number.range
and some even include the last value.0.1.to(2.3, 0.2)
does not include 2.3
. (That's what expected for floating point operations. And nothing we can do here.)About the conflict.
This problem is different from Array#flatten
.
They (Mootools) enumerate on the Array.prototype
and flatten is not copied.
I don't think this will happen on Number.range
. Is anyone write for (var x in Number)
?
If anyone use their own Number.range
, native one will be overwrited.
Reference: https://developers.google.com/web/updates/2018/03/smooshgate
It could still cause a problem if someone’s Number.range conditionally overwrites, and has different semantics.
Alright. Not on Number.prototype
. I found someone enumerate on Number.prototype
.
Update: Oh he didn't use range
in his code.
We could leave it to bind operator / extensions.
1::to(3) // Number.range(1, 3 + 1)
1::until(3) // Number.range(1, 3)
How about Scala way? 3 .to(5) // 3, 4, 5 (3).to(5) // same, some may prefer stylistically 3..to(5) // same, some may prefer stylistically 3.1.to(5.1) // 3.1, 4.1, 5.1 // beware of rounding, paradoxes ahead! 3 .until(4.1) // 3, 4 3.1.until(5.1) // 3.1, 4.1 // beware of rounding, paradoxes ahead! 9 .to(6, -1) // 9, 8, 7, 6 9 .to(7, -2) // 9, 7 9 .until (7, -2) // 9 1 .to(1, 0) // infinite sequence of 1s 1 .until(1, 0) // nothing Infinity.to(Infinity) // Infinity Infinity.until(Infinity, 0) // nothing Infinity.to(7, -3) // infinite sequence containing only Infinity
Possibly also with detection of direction when second argument is something special (NaN, "s" (my preference), some symbol).
If the first argument is undefined, it should be treated as -Infinity if the second one is negative and Infinity otherwise. The latter's default value should be 1.
This doesn't specially address the common 0 case but "0 .to" seems short enough.