Full closures for D

outersky outersky at gmail.com
Tue Nov 6 19:28:10 PST 2007


Great!

Since it's a delegate, can it be replaced as a Closure ? If closure can 
support parameters.


Russell Lewis 写道:
> 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); };
> }
> 
> outersky wrote:
>> Maybe the two inner variables can be omitted  as :
>>
>>
>> C delegate(B) curry(A, C, B...)(C delegate(A, B) dg_, A a_) {
>>   C add(B b) {
>>     return dg_(a_, b);
>>   };
>>   return &add;
>> }
>>
>>
>> Witold Baryluk 写道:
>>> Dnia Fri, 02 Nov 2007 14:03:59 -0700
>>> Walter Bright <newshound1 at digitalmars.com> napisał/a:
>>>
>>>> D 2.007 brings full closures to D. I was tired of D being denigrated
>>>> for not having "real" closures.
>>>>
>>>
>>> Simpler curring?
>>>
>>> // not tested
>>> C delegate(B) curry(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;
>>> }
>>>
>>>
>>> instand of:
>>>
>>> // Old code:  http://www.digitalmars.com/d/template.html
>>>
>>> R delegate(U) Curry(R, A, U...)(R delegate(A, U) dg, A arg)
>>> {
>>>     struct Foo
>>>     {
>>>     typeof(dg) dg_m;
>>>     typeof(arg) arg_m;
>>>
>>>     R bar(U u)
>>>     {
>>>         return dg_m(arg_m, u);
>>>     }
>>>     }
>>>
>>>     Foo* f = new Foo;
>>>     f.dg_m = dg;
>>>     f.arg_m = arg;
>>>     return &f.bar;
>>> }
>>>
>>>
>>>
>>> Also other functional stuff like infinite lazy lists or monads can be
>>> done much much simpler now :)
>>>



More information about the Digitalmars-d mailing list