A currying function
Artur Skawina
art.08.09 at gmail.com
Thu Jun 21 15:12:57 PDT 2012
On 06/21/12 21:04, Philippe Sigaud wrote:
> On Tue, Jun 19, 2012 at 2:52 PM, Artur Skawina <art.08.09 at gmail.com> wrote:
>
>> I'm not really sure when you'd want to use this in D.
>
> As for Haskell. For example, with a range of ranges and you want to
> take the first 100 elements of all subranges:
>
> alias Curry!take ctake;
>
> auto target = [[0,1,2,...], [...], ];
> auto first100s = map!(ctake(100))(target);
>
> And so on. When mapping and filtering ranges, I need currying from
> time to time.
Sure, but partial application would be enough for cases like this one.
auto partappe(alias F, A...)(auto ref A a) {
auto ref f(ParameterTypeTuple!F[0..$-A.length] b) { return F(b, a); }
return &f;
}
int n = 2;
auto target = [[0,1,2], [3,4,5], [6,7,8]];
auto takeN = partappe!(take!(int[]))(n);
auto firstN = map!takeN(target);
I wonder if there's a case for "real" currying in 'D'; still can't think
of one, but maybe that's just because it's not how i would usually code it.
artur
More information about the Digitalmars-d
mailing list