4
K-ASS
2y

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!

Comments
  • 2
    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.
  • 0
    The javascript example might look like the attached figure. There may be off-by-one errors, I didn't actually run this
  • 0
    @lbfalvy I’m actually trying to go for an example like list(range(n)) which is kinda useful
  • 0
    I linked the article about range(), you might want to encode the parentheses in titles because url parsers just suck.
  • 0
    @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
  • 0
    Or just (lower, upper) => new Array(upper - lower).map((n, i) => i + lower) which involves one less array allocation.

    Edit: accidental Rust lambda syntax
  • 0
    @lbfalvy in python I rarely use range somehow, I’m more for list comprehension, which is actually faster
  • 0
    .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
  • 1
    @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.
  • 0
    @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
  • 0
    Looks nice.
Add Comment