[OT] Separation of Functionality in C (was: Re: how to write a C-style for loop?)

Dan Sommers me at privacy.net
Wed Feb 15 19:07:00 EST 2006


On 15 Feb 2006 12:48:11 GMT,
Duncan Booth <duncan.booth at invalid.invalid> wrote:

> Putting the question the other way round: how would you move the
> control logic out of the loop in C?

I have written many functions not unlike this one (untested):

    void for_each_record( void (*callback)( RECORD * ), void *callback_data )
    {
        DATA *p;

        for( p = the_list; p; p = p->next )
            (*callback)( p->the_record, callback_data );
        return;
    }

where "the_list" comes from somewhere (probably static within the module
containing for_each_record, or the loop could access a database, or
whatever), and it is up to the callers to write their own callback
functions and manage their own callback_data.  Obviously, in an actual
product, there would be error checking, options for the callback
function to abort the loop early, and other bells and whistles (e.g.,
logging options) depending on the application.

But now we're off topic for comp.lang.python.

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>



More information about the Python-list mailing list