by BiteCode_dev · 4y ago · 0 comments
Of course, don't use your own hand crafted version in Python, since one is provided with the language and it is better than the one you will likely come up with.
Still, it's interesting because it's simple. So starting from the intro of this article, if you want to go further, try to do the following to match up __builtins__.zip:
- make it lazy (yield would work)
- make it work with any iterable (iter() + StopIteration would work)
- put some type int and a docstring (so that help() gives you informations)
- accept any number of iterables (variadic parameters would work)
- make a second version that accepts a fill value (like itertools.zip_longest)
This will sharpen your python skills, and also will make you appreciate even better how much you get for free with the stdlib (not to mention __builtins__.zip is in C, so it's fast).
>>> from operator import `add`
>>> list(map(add, range(5), range(10, 15)))
[10, 12, 14, 16, 18]
>>> add3 = lambda x,y,z: x+y+z
>>> list(map(add3, range(5), range(10, 15), range(100, 105)))
[110, 113, 116, 119, 122]This is unironically the reason I come back to Python again and again. The language itself is not my favorite: it 'should' be different. But my productivity with it is frankly unmatched.
The "zip" example is an excellent case study. The actual "zip" is by default lazy, it accepts arbitrary iterators, etc. It's the "correct" solution, unlike the one presented in the article, and I have it at my fingertips, in the standard library.
Can I ask an off topic question? Where did this sudden popular use of “unironically” come from? I don’t get it. Why would it be ironic?
It reminds me of the sudden rise of “literally” a decade or so ago.
Yes, it's not really antonymous to "ironically" (in the narrower sense of this word) in this context.
But this is also how word meanings shift over time. Someone will see someone else using that word and not know what it means, but then start using it, etc.
It's not hard to implement just about any protocol on top of that abstraction, e.g. you have itertools.zip_longest()
It really is not. It’s just a natural outcome of imperative langages. If operations can have side-effects, then combinator behaviour will affect those side-effects.
You could do the same with iterables with side-effecting iterations.
It’s also perfectly logical to be able to iterate an iterator.