4 Jun 2009 15:37
Making the case for repeat
pataphor <pataphor <at> gmail.com>
2009-06-04 13:37:45 GMT
2009-06-04 13:37:45 GMT
This probably has a snowballs change in hell of ending up in builtins or
even some in some module, but such things should not prevent one to
try and present the arguments for what one thinks is right. Else one
would end up with consequentialism and that way lies madness and
hyperreality.
So here is my proposed suggestion for a once and for all reconciliation
of various functions in itertools that can not stand on their own and
keep a straight face. Because of backwards compatibility issues we
cannot remove them but we can boldly jump forward and include the right
repeat in the builtin namespace, which I think would be the best thing.
Alternatively -- the second best solution -- would be to give this
function its own namespace where it can supersede the old incongruencies
in itertools. Combiniter or combinator?
P.
from itertools import count
from functools import partial
def repeat(iterable, cycles = None, times = 1):
L = []
for x in iterable:
for i in xrange(times):
yield x
L.append(x)
counter = count if cycles is None else partial(xrange,cycles-1)
for _ in counter():
for x in L:
for i in xrange(times):
(Continue reading)
Enticing and yet fallacious in its
presumption of known and accepted usability problems. FWIW, when I
designed the module, I started by researching constructs that had
proven success in functional languages and then adapted them to the
needs of Python applications. That being said, I'm always open to
hearing new ideas.
After reading this thread a couple times, I have a few thoughts
to offer.
1. The Pythonic Way(tm) is to avoid combining too much functionality
in a single function, preferring to split when possible. That is why
ifilter() and ifilterfalse() are separate functions.
(FWIW, the principle is considered pythonic because it was articulated
by Guido and has been widely applied throughout the language.)
There is a natural inclination to do the opposite. We factor code
to eliminate redundancy, but that is not always a good idea with
an API. The goal for code factoring is to minimize redundancy.
The goal for API design is having simple parts that are easily
learned and can be readily combined (i.e. the notion of an
iterator algebra).
It is not progress to mush the parts together in a single function
RSS Feed