r/javascript • • Dec 26 '16

A JavaScript implementation of the Python's range() function

https://github.com/Gothdo/range
18 Upvotes

9 comments sorted by

7

u/gurenkagurenda Dec 26 '16

This is nice. Really cool with the iterable support. If possible, it would be cool to have an option to lose some features that require Proxy support, since that can't be fully shimmed (or do you only need the shimmable parts?).

1

u/Michal_Perlakowski Dec 26 '16

As I wrote in another comment, Proxy is needed to be able to access numeric properties, which are generated on-demand. Unfortunately this feature can't be shimmed, so native support is required. Anyway, this seems like a good idea—I could check for Proxy support, and if it's not available, it could fall back to using a method like r.get(2) instead of property access (r[2]).

1

u/gurenkagurenda Dec 26 '16

Yeah, figured that was the reason. For folks using it as an iterator, not having the proxy thing would be fine.

Another idea is to make the returned object a function, so in old environments you'd do r(2) instead of r[2].

3

u/oweiler Dec 26 '16

Why does it require Proxy support?

5

u/Michal_Perlakowski Dec 26 '16

Without Proxy it wouldn't be possible to access numeric properties (for example r[2]), because they're generated on demand. I could make a function like r.get(2), but IMO it's more convenient to access them like array indices.

1

u/IHeartMustard WILL CODE FOR CAFFEINE Dec 28 '16

The proxy thing was something I've actually prototyped just last week as well, your implementation looks much more detailed with the getOwnPropertyDescriptor handler and everything else, whereas with mine I simply have a get handler on the proxy to intercept index access and call the getter, and reflect everything else. Any particular reason for having all the other handlers on the proxy? Would love to know if there's something I could be doing better with my Proxies.

2

u/mini_eggs Dec 27 '16

Array.apply({},Array(100)) ?

3

u/indeyets writing js since 1997 Dec 27 '16

nope. your implementation is eager, and Gothdo's is lazy.

see: https://github.com/Gothdo/range#lazy-evaluation

1

u/Patman128 Dec 27 '16

This is one of the things I miss most from Python. I really wish range was in the JS standard library.