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

Tab Atkins Jr. jackalmage at gmail.com
Wed Nov 28 21:33:36 UTC 2018


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


More information about the es-discuss mailing list