Re: Making the case for repeat



[pataphor]
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.

Interesting phraseology ;-) 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
requiring multiple parameters.

2. I question the utility of some combining repeat() and cycle()
because I've not previously seen the two used together.

OTOH, there may be some utility to producing a fixed number of cycles
(see the ncycles() recipe in the docs). Though, if I thought this
need
arose very often (it has never been requested), the straight-forward
solution would be to add a "times" argument to cycle(), patterned
after repeat()'s use of a "times" argument.

3. Looking at the sample code provided in your post, I would suggest
rewriting it as a factory function using the existing tools as
components. That way, the result of the function will still run
at C speed and not be slowed by corner cases or unused parameters.
(see the ncycles recipe for an example of how to do this).

4. The suggested combined function seems to emphasize truncated
streams (i.e. a fixed number of repetitions or cycles). This is
at odds with the notion of a toolset designed to allow lazy
infinite iterators to be fed to consumer functions that truncate
on the shortest iterable. For example, the toolset supports:

izip(mydata, count(), repeat(datetime.now()))

in preference to:

izip(mydata, islice(count(), len(mydata)), repeat(datetime.now
(),times=len(mydata)))

To gain a better appreciation for this style (and for the current
design of itertools), look at the classic Hughes' paper "Why
Functional
Programming Matters".

http://www.math.chalmers.se/~rjmh/Papers/whyfp.pdf


Raymond

.



Relevant Pages

  • Re: Bizzare behaviour from SG/UC3525
    ... >> The application is a high power pro-audio amplifier. ... It's not a type of design that neatly falls into ... > In particular the shutdown pin is not "slow acting" like you have theorized. ... > next cycle of the oscillator. ...
    (sci.electronics.design)
  • Re: Superscalar Out-of-Order Processor on an FPGA
    ... Only when I have some design which I believe would be reasonable I start ... Cycle C tells me right away that the architecture does what its ... I do recall my days at Inmos where the Transputer was all over very ... and much of it before any gate level simulation decades before ...
    (comp.arch.fpga)
  • Re: Six Stroke Engine
    ... The clever new six-stroke design was developed by 75-year-old mechanic ... owner of a company which produces high-performance cams and other engine ... the water immediately ... and then the six-stroke cycle begins again. ...
    (misc.rural)
  • Re: Using SSR to switch transformer
    ... The waveforms I referenced were created on purpose to test the effects of bad timing, and to demonstrate the permanent magnetization of the core. ... I designed a product a year ago that uses a saturating magnetic device as an automatic tap changer, and went through much this same process, although at the design stage as I was aware of it beforehand. ... I have a lovely scope shot of this happening, with a 1 Ohm Rdson FET dropping 100V just as it shits itself - the first cycle has a tiny surge, but a full cycle later the core slams into saturation and the current skyrockets, followed 100us or so later by catastrophic failure. ...
    (sci.electronics.design)
  • Re: [PATCH 1/6] new timeofday core subsystem for -mm (v.B3)
    ... On Mon, 20 Jun 2005, john stultz wrote: ... for a reasonable range of timesource frequencies and interval ... the cycle values needs to be 64 bits wide and the mult/shift ... What's exactly wrong with old design and why wasn't possible to fix it ...
    (Linux-Kernel)