News for hackers. Stuff that matters.
Re:Python: Uncovering the Overlooked Core Functionalities by gcanyon on Tuesday July 25, 2023 @02:22AM (#36858649)
I think I figured out that count(2) is from itertools? I'm new to python.

I think you could simplify the rest like so:

    def primesHN():
        from collections import defaultdict
        from itertools import count
        yield(2)
        ps = defaultdict(list)
        for i in count(3,2):
            if i not in ps:
                yield(i)
                ps[i**2].append(2*i)
            else:
                for n in ps.pop(i):
                    ps[i + n].append(n)
Re:Python: Uncovering the Overlooked Core Functionalities by qsort on Tuesday July 25, 2023 @03:23AM (#36859068)
> I think I figured out that count(2) is from itertools?

It is. Itertools is a masterpiece of a module. It has a lot of functions that operate on iterators and will work both on standard iterables (lists, tuples, dicts, range(), count() etc.) and on your own generators. It forms a sort of "iterator algebra" that makes working with them very easy.

> I think you could simplify the rest like so:

Sounds good, but with a caveat: you do need to call "del" at the end for memory deallocation purposes. The garbage collector isn't smart enough to know you won't be using those dictionary entries any longer. Technically the code still works, but keeping everything in memory defeats the purpose of writing a generator.

Re:Python: Uncovering the Overlooked Core Functionalities by gcanyon on Tuesday July 25, 2023 @05:16PM (#36869719)
> you do need to call "del" at the end

The garbage collector doesn't understand "pop"? That seems...dumb? ¯\_(ツ)_/¯