Re: Python from Wise Guy's Viewpoint
prunesquallor@c omcast.net once said:[color=blue]
>gt5163b@prism. gatech.edu (Brian McNamara!) writes:[color=green]
>> 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);
>> }
>>[/color]
>
>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?[/color]
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_appl y" so well. I expect that Haskell can
mimic it better in this respect.
[color=blue]
>What if F `returns' void?[/color]
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@acm.org : I am a parsing fool!
** Reduce - Reuse - Recycle ** : (Where's my medication? ;) )
prunesquallor@c omcast.net once said:[color=blue]
>gt5163b@prism. gatech.edu (Brian McNamara!) writes:[color=green]
>> 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);
>> }
>>[/color]
>
>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?[/color]
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_appl y" so well. I expect that Haskell can
mimic it better in this respect.
[color=blue]
>What if F `returns' void?[/color]
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@acm.org : I am a parsing fool!
** Reduce - Reuse - Recycle ** : (Where's my medication? ;) )
Comment