Full closures for D
Bill Baxter
dnewsgroup at billbaxter.com
Tue Nov 6 20:46:53 PST 2007
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: currying an argument actually means creating a function
that accepts a function of N arguments and returns a function that
accepts 1 argument and returns a function that accepts N-1 arguments.
But I don't really care if you want to call partial application
currying. Just be aware that you're using the term in a way many would
deem incorrect.
Random link: http://srfi.schemers.org/srfi-26/mail-archive/msg00015.html
--bb
More information about the Digitalmars-d
mailing list