New Proposal: Number.range (Yes, range again)

Jack Works zjwpeter at gmail.com
Thu Nov 29 04:28:49 UTC 2018


Oh, I'm not here to find out how to implement a range in detail. That's the
trivial things, I want to first make it become a stage 0 proposal, then we
can discuss if we need some overload or how to deal with the edge cases.
Ps, does Pratt mean we should add it as a standard library in the stage 1
standard libraray proposal?

On Thu, Nov 29, 2018, 12:18 Jacob Pratt <jhprattdev at gmail.com> wrote:

> Quick, simple TypeScript range function (with overloads, of course).
>
> ```
> export function range(end: number): IterableIterator<number>;
> export function range(start: number, end: number):
> IterableIterator<number>;
> export function range(start: number, end: number, step: number):
> IterableIterator<number>;
> export function* range(start: number, end?: number, step?: number):
> IterableIterator<number> {
>   // overload #1
>   if (end === undefined) {
>     [start, end, step] = [0, start, 1];
>   }
>
>   // overload #2
>   if (step === undefined) {
>     step = Math.sign(end - start);
>   }
>
>   // ensure we have the appropriate types
>   if (typeof start !== 'number' || typeof end !== 'number' || typeof step
> !== 'number') {
>     throw new TypeError('all parameters must be of type number');
>   }
>
>   while ((start < end && step > 0) || (start > end && step < 0)) {
>     yield start;
>     start += step;
>   }
> }
> ```
>
> IMO, we should focus on building up a JavaScript standard library that has
> tons of useful utilities like this, rather than continue to add methods
> onto namespaces. Perhaps that's just me, though.
>
> Jacob Pratt
>
> On Wed, Nov 28, 2018 at 9:33 PM Tab Atkins Jr. <jackalmage at gmail.com>
> wrote:
>
>> I end up writing a range function in virtually every project I use, so
>> yeah, I think this is worthwhile.
>>
>> My own preferences, based on Python precedence:
>>
>> Number.range(end) === Number.range(0, end, 1)
>> Number.range(start, end) === Number.range(start, end,
>> Math.sign(end-start))
>> Number.range(start, end, step) => {
>>   if start < end && step > 0: yield all (start + k*step), for k from 0
>> to infinity, less than end
>>   elif start > end && step < 0: yield all (start + k*step), for k from
>> 0 to infinity, greater than end
>>   else: yield nothing
>> }
>>
>> So:
>> * [...Number.range(5)] evaluates to [0, 1, 2, 3, 4]
>> * [...Number.range(0)] evaluates to []
>> * [...Number.range(-5)] evaluates to []
>> * [...Number.range(1, -3)] evaluates to [1, 0, -1, -2]
>> * [...Number.range(-3, 1)] evaluates to [-3, -2, -1, 0]
>> * [...Number.range(1, 1)] evaluates to []
>> * [...Number.range(0, 10, 5)] evaluates to [0, 5]
>> * [...Number.range(0, 10, -5)] evaluates to []
>>
>> ~TJ
>>
> _______________________________________________
>> 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/20181129/45b295b8/attachment.html>


More information about the es-discuss mailing list