
Boost : 
Subject: Re: [boost] painless currying
From: Larry Evans (cppljevans_at_[hidden])
Date: 20110903 10:48:34
On 09/03/11 08:28, Larry Evans wrote:
> On 08/23/11 14:38, Eric Niebler wrote:
>> After playing around with functional languages, I've come to envy how
>> easy they make it to curry functions. Call a 2argument function with 1
>> argument and you get a function that takes 1 argument. Pass another
>> argument and it evaluates the function. Simple. In contrast, C++ users
>> have to use binders, which are not as nice.
>>
>> On a lark, I implemented a wrapper that turns any TR1style function
>> object into a "curryable" function object (attached). Successive
>> function call invocations bind arguments until enough arguments are
>> bound to invoke the wrapped function. With it you can do the following:
>>
>> curryable<std::plus<int> > p;
>> auto curried = p(1);
>> int i = curried(2);
>> assert(i == 3);
>>
>> Is there any interest in such a thing?
>
> With the 2 attached files (compiled with gcc4.6 and with boost trunk
> [which svn update showed was revision 74198]), the output is:
[snip]
With the revised curryable.cpp, the output is:
/home/evansl/prog_dev/boostsvn/ro/trunk/sandboxlocal/build/gcc4_6v/boostsvn/ro/trunk/sandbox/rw/variadic_templates/sandbox/painless_currying/curryable.exe
test<1>:
type_u<0>()
type_u<0,1>(type_u const&)
type_u<0,1>(type_u const&)
type_u<1>()
type_u<1,1>(type_u const&)
type_u<0,1>(type_u const&)
type_u<0,1>(type_u const&)
type_u<1,1>(type_u const&)
type_u<2>()
sout<<type_u<0>
sout<<type_u<1>
sout<<type_u<2>
test<0>:
type_u<0>()
type_u<0,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1>()
type_u<1,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2>()
type_u<2,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<3>()
type_u<3,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<3,0>(type_u const&)
type_u<4>()
type_u<4,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<3,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<3,0>(type_u const&)
type_u<4,0>(type_u const&)
type_u<5>()
type_u<5,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<3,0>(type_u const&)
type_u<4,0>(type_u const&)
type_u<0,0>(type_u const&)
type_u<1,0>(type_u const&)
type_u<2,0>(type_u const&)
type_u<3,0>(type_u const&)
type_u<4,0>(type_u const&)
type_u<5,0>(type_u const&)
Compilation finished at Sat Sep 3 09:41:23
which reveals that, when passed arguments that cannot
possibly be valid, the arguments just keep accummulating
in the curryable<F,Args...>::argI. It would be helpful
if some diagnostic were issued in case too many arguments
are supplied.
regards,
Larry
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk