4 ms·
'range' a-la python
by amitport 5y ago
'range' a-la python
- wruza 5y agoRange a-la python is a generator and is pretty straightforward in js as is. There is no need for a complicated solution and/or as part of stdlib, imo: function* range(x, y) { while (x <= y) {yield x++} // or ‘<‘ if you want half-open one } for (let n of range(3, 5)){ console.log(n) } array = [...range(3, 5)] console.log(array)
- amitport 5y agoIt's also straight forward to implement in python. It is common enough to be standard so people won't reinvent the wheel. And it should be available to people learning the language before they learn generators. Array.prototype.indexOf is also pretty simple as are many core functions.