Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save alphaKAI/89f897225df423aa26cc to your computer and use it in GitHub Desktop.

Select an option

Save alphaKAI/89f897225df423aa26cc to your computer and use it in GitHub Desktop.
D言語でカリー化実装してみた
import std.stdio;
template curry(alias func){
import std.algorithm, std.conv, std.range, std.traits;
alias argsintuple = ParameterTypeTuple!func;
immutable lambdaStr = (lamArgs =>
(temp){
foreach(i, e; argsintuple)
temp ~= "(" ~ e.stringof ~ " " ~ lamArgs[i] ~ ") => ";
return temp ~ "func(" ~ lamArgs.join(", ") ~ ")";
}("")
)(argsintuple.length.iota.map!(i => "arg" ~ i.to!string));
enum curry = mixin(lambdaStr);
}
int f(int x, int y, int z){
return x * y * z;
}
void main(){
auto fc = curry!f;
assert(fc(1)(2)(3) == f(1, 2, 3);
}
@e10s
Copy link
Copy Markdown

e10s commented Mar 16, 2015

どや!

template curry(alias func){
  import std.algorithm, std.conv, std.range, std.traits;
  alias argsTuple = ParameterTypeTuple!func;

  immutable lambdaStr = (lamArgs =>
    (temp){
      foreach(i, e; argsTuple){
        temp ~= "(" ~ e.stringof ~ " " ~ lamArgs[i] ~ ") => ";
      }
      return temp ~ "func(" ~ lamArgs.join(", ") ~ ")";
    }("")
  )(argsTuple.length.iota.map!(i => "arg" ~ i.to!string));

  // pragma(msg, lambdaStr);
  enum curry = mixin(lambdaStr);
}

int f(int x, int y, int z){
  return x * y * z;
}

void main(){
  import std.stdio;
  typeof(curry!f).stringof.writeln;
  assert(curry!f(2)(3)(5) == f(2, 3, 5));
}

@alphaKAI
Copy link
Copy Markdown
Author

Thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment