Full closures for D

Bill Baxter dnewsgroup at billbaxter.com
Tue Nov 6 20:47:58 PST 2007


Bill Baxter wrote:
> Witold Baryluk wrote:
>> Dnia Tue, 06 Nov 2007 20:17:58 -0700
>> Russell Lewis <webmaster at villagersonline.com> napisał/a:
>>
>>> Or even:
>>>
>>> C delegate(B) curry(A, C, B...)(C delegate(A, B) dg_, A a_) {
>>>    return delegate C(B b) { return dg_(a_, b); };
>>> }
>>
>> Yes i was thinking about it, but didn't know if it should work.
>> I just installed 2.007, and it is working! :)
>>
>>
>> import std.stdio;
>>
>> C delegate(B) curry1(A, C, B...)(C delegate(A, B) dg_, A a_) {
>>         auto a = a_;
>>         auto dg = dg_;
>>         C add(B b) {
>>                 return dg(a, b);
>>         }
>>         return &add;
>> }
>>
>> C delegate(B) curry2(A, C, B...)(C delegate(A, B) dg, A a) {
>>         C add(B b) {
>>                 return dg(a, b);
>>         }
>>         return &add;
>> }
>>
>> C delegate(B) curry3(A, C, B...)(C delegate(A, B) dg, A a) {
>>         return delegate C(B b) { return dg(a, b); };
>> }
>>
>>
>>
>> int main(char[][] args) {
>>         int sum(int a, int b, int c) {
>>                 return a+b+c;
>>         }
>>
>>         auto s1 = curry1(&sum, 11000);
>>         assert(s1(200,30) == 11230);
>>
>>         auto s2 = curry2(&sum, 21000);
>>         assert(s2(200,30) == 21230);
>>
>>         auto s3 = curry3(&sum, 31000);
>>         assert(s3(200,30) == 31230);
>>
>>         return 0;
>> }
> 
> Pedantic's note: 

Pedant's note: a pedantic person is called a "pedant".

--bb



More information about the Digitalmars-d mailing list