Full closures for D
Witold Baryluk
baryluk at smp.if.uj.edu.pl
Tue Nov 6 20:37:49 PST 2007
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;
}
--
Witold Baryluk, aleph0
MAIL: baryluk at smp.if.uj.edu.pl
JID: movax at jabber.autocom.pl
More information about the Digitalmars-d
mailing list