En Thu, 04 Jun 2009 10:37:45 -0300, pataphor <patap...@gmail.com> escribió:

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?

Ok, you're proposing a "bidimensional" repeat. I prefer to keep things
simple, and I'd implement it in two steps. First, something similar to
your repeat_each function in another post:

py> thing = ['1','2','3','4']
py> chain.from_iterable(repeat(elem, 3) for elem in thing)
<itertools.chain object at 0x00BECB90>
py> list(_)
['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4']

Note that this doesn't require any additional storage. Second step would
be to build a bidimensional repeat:

py> one = chain.from_iterable(repeat(elem, 3) for elem in thing)
py> two = chain.from_iterable(tee(one, 2))
py> list(two)
['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4', '1', '1',
'1', '2',
  '2', '2', '3', '3', '3', '4', '4', '4']

Short and simple, but this one requires space for one complete run (3*4
items in the example).
Another variant that only requires space for freezing the original
iterable (4 items in the example) is:

thing = ['1','2','3','4']
items = list(thing) # ok, silly in this case, but not for a generic iterable chain.from_iterable(chain.from_iterable(repeat(elem, 3) for elem in items) f
or rownumber in range(2))
<itertools.chain object at 0x00BEC7D0>
list(_)
['1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4', '1', '1',
'1', '2',
  '2', '2', '3', '3', '3', '4', '4', '4']

All of them run at full speed, using the optimized itertools machinery
written in C.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to