Last active
August 29, 2015 14:17
-
-
Save alphaKAI/89f897225df423aa26cc to your computer and use it in GitHub Desktop.
D言語でカリー化実装してみた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
どや!