Use case for std.bind

Lars Kyllingstad public at kyllingen.NOSPAMnet
Tue Feb 24 05:11:21 PST 2009


Yigal Chripun wrote:
> downs wrote:
>> Yigal Chripun wrote:
>>> Lars Kyllingstad wrote:
>>>> I've always thought currying was the main point of std.bind. If I'm not
>>>> mistaken, currying is commonly a built-in feature of functional
>>>> programming languages, so if anything, std.bind could become more
>>>> useful/important in D2 than in D1.
>>>>
>>>> I agree that the std.bind API could and should be improved.
>>>>
>>>> -Lars
>>> you don't need bind for currying, it's even possible to do this in C:
>>>
>>> int foo(int a, int b) { ... }
>>> int bar(int a) { return foo(a, _value); } // curry with some _value
>>>
>>> Other languages provide useful syntax sugar for currying:
>>> auto bar2 = foo(_, 500);
>>>
>>> bar2 here will be a delegate that will do the the same as the above bar.
>>
>> Just for comparison' sake:
>>
>> auto dg =&foo /rfix/ somevar;            // 1.0, tools
>> auto dg = _bind(&foo, _1, somevar);        // 1.0, std.bind
>> auto dg = (int a) { return foo(a, somevar); };    // 2.0, literal
>   auto dg = foo(_, somevar);
> 
> So does that mean you like the above suggestion?
> after all, it's shorter and clearer than all the other alternatives.. ;)

I for one like it, and would very much like to see such a syntax in D2. 
Also, I'd have it work with either of a function's arguments, not only 
the leading or trailing ones:

   int foo(int a, int b, int c, int d) {...}
   auto bar1 = foo(_, b, c, d);
   auto bar2 = foo(a, b, c, _);
   auto bar3 = foo(_, b, _, d);

   assert (is (typeof(bar3) == int delegate(int, int)));

In its simplest form, it would just be syntactic sugar for the delegate 
literal. But I'm curious: If foo is a pure function, couldn't the 
compiler perform some extra optimization here?

-Lars



More information about the Digitalmars-d mailing list