Python from Wise Guy's Viewpoint

Brian McNamara! gt5163b at prism.gatech.edu
Sat Oct 25 14:10:57 EDT 2003


prunesquallor at comcast.net once said:
>gt5163b at prism.gatech.edu (Brian McNamara!) writes:
>> Well, in C++ you could say
>>
>>    template <class F, class A>
>>    typename result_of<F(A)>::type
>>    noisy_apply( const F& f, const A& a ) {
>>       cout << "I am now about to apply " << f << " to " << a << endl;
>>       return f(a);
>>    }
>>
>
>I don't mean to nitpick, but APPLY takes an arbitrary list of arguments.
>How do you parameterize over that without enumerating the power set
>of potential types?

This isn't really possible for normal C++ functions.

You can always program in a style where every function takes exactly
one argument, which is an N-ary tuple, and use boost::mpl and
boost::tuple to then generalize things.  (Indeed, using such libraries,
you can simulate "apply" rather convincingly.  But somewhere under the
hood, someone has to have written N different overloads for 0-arg,
1-arg, ... N-arg, up to some fixed ("large enough") N.)

So C++ can only mimic "noisy_apply" so well.  I expect that Haskell can
mimic it better in this respect.

>What if F `returns' void?

It still works.  (You are allowed to say "return f(a)" inside a template
function returning void, provided f(a) "returns" void as well.)

-- 
 Brian M. McNamara   lorgon at acm.org  :  I am a parsing fool!
   ** Reduce - Reuse - Recycle **    :  (Where's my medication? ;) )




More information about the Python-list mailing list