Boost logo

Boost :

From: Andy Glew (glew_at_[hidden])
Date: 1999-06-11 14:55:03


> >[ Messy adaptators naming conventions ]
> >
> >> Functionality Boost Name SGI STL's Name
> >> f(g(value)) compose11 compose1
> >> f(g(value),h(value)) compose21 compose2
> >> f(g(value1),h(value2)) compose22
> >> f(g(value1,value2)) compose12
> >> f(g()) compose10
> >
> >All this mess IMHO leads to:
> >
> >1) almost unreadable code
> > ...
>
> Agreed. See if you can come up with a better approach to functional
> programmming in C++ without changing the language. Who knows, you
> might become famous and get your name in lights!

Hmmm.... A while back I wrote a preprocessor that accepted lambda
functions - basically by taking lambda functions in code, and creating
a static definition somewhere else.

e.g. something like

        typedef intfunc f(int);

        foo() {
                intfunc* lambdaptr = int lambda(int a) { return a+1; }
                ...
                int b = lambdaptr(25);
                ...
        }

getting transmogrified into

        typedef intfunc f(int);
        ...
        int lambda_9899 (int a) { return a+1; }
        ...
        foo() {
                ...
                intfunc* lambdaptr = lambda_9899;
               ...
                int b = lambdaptr(25);
                ...
        }

This was limited in that it could only handle "static"
lambda functions - it could not properly handle lambda functions
that made references in their body to non-global variables.

For all that, it was quite useful - and if this lambda function notation
is combined with C++ functor object approach, even more can be done.

Once you have lambda function notation, all of the problems in creating
names for functional manipulators disappears.

>> f(g(value)) compose11 compose1

        composefunc = T lambda( T value ) { return f( g( value )); }

    OR, if f and g are themselves function pointers, define an object
    to hold them

                template< class T> class function_composition_2_internal {
                    T (*f)(T);
                    T (*g)(T);
                    T operator(T value) { return f ( g ( value ) ) ; }
                }

    You can have the lambda function preprocessor create such an object
    by parsing the lambda function body to recognize externals,
    appropriately initialized at the point of lambda function declaration,
    or it might be easier to explicitly provide them as parameters.

>> f(g(value),h(value)) compose21 compose2

        composefunc = T lambda( T value ) { return f( g(value), h(value) ); }

Etc.

Since a lambda is a declaration returning either a function pointer or,
more generally, a functor, you can use them wherever C++'s klugey function
composition and hasseling library functions are used.

Yeah, this would be a change in the language. But it would be a small change,
that avoids the need for a plethora of named functional composition operators.

------------------------------------------------------------------------

eGroups.com home: http://www.egroups.com/group/boost
http://www.egroups.com - Simplifying group communications


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk