Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
First thing I notice, on this page:
https://txstc55.github.io/But-how-d...()%20Function%20in%20Languages%20Other%20Than%20Python
you're allocating an array of size N in JS and I think the same is true for C++ but I'm not sure. O(N) space complexity and a potentially large heap allocation just for iterating over integers is unacceptable. The point of range() in python is that it's an iterator and next() just does the same thing a classic for loop would. C++ has first class iterator support with for-in loops (the syntax is with a colon but it's basically the same), and Javascript even has generator functions, so it's more than possible to solve this elegantly in both languages. -
K-ASS25452y@lbfalvy I’m actually trying to go for an example like list(range(n)) which is kinda useful
-
I linked the article about range(), you might want to encode the parentheses in titles because url parsers just suck.
-
@K-ASS I'm always suspicious when I see that pattern in Python, most people who write stuff like that do so because they're afraid of iterators. You can also always do [...range(n)] in JS if you use my range function
-
Or just (lower, upper) => new Array(upper - lower).map((n, i) => i + lower) which involves one less array allocation.
Edit: accidental Rust lambda syntax -
K-ASS25452y@lbfalvy in python I rarely use range somehow, I’m more for list comprehension, which is actually faster
-
.keys() is also a virtual function, in this case it should break down into the single array of 1..n due to known type, inlining and very obvious iterator optimizations, but for me personally methods of Object are always extra cognitive load
-
@K-ASS I rarely use it as well, the only context where I need integers usually is for indices and then enumerate() is less confusing. But when I do, it's only ever as an iterator, not a list.
-
K-ASS25452y@lbfalvy true, but for my other projects, they usually represent an index position which is prune to change, and this is where this code snippets comes handy
Related Rants
After contemplating on it so long, I finally made a blog for code snippets:
https://txstc55.github.io/But-how-d...
Completely written by myself, except, I mean, the vuejs and tailwind css part.
It’s so beautiful
Open to suggestions for code snippets to add!
rant
blog
side project