Skip to content

Instantly share code, notes, and snippets.

@9rnsr
Forked from majiang/functionstyle.d
Created June 5, 2012 04:04
Show Gist options
  • Save 9rnsr/2872562 to your computer and use it in GitHub Desktop.
Save 9rnsr/2872562 to your computer and use it in GitHub Desktop.
function style
import std.stdio;
void main()
{
T delegate(T) zero(T)(T delegate(T) f)
{
return x => x;
}
T delegate(T) delegate(T delegate(T)) succ(T)(T delegate(T) delegate(T delegate(T)) n)
{
return f => x => f(n(f)(x));
}
auto n = &zero!uint;
foreach (i; 0..10)
{
writefln("i = %d: f(0) = %d", i, n(x => x + 1)(0));
assert(n(x => x + 1)(0) == i);
n = succ(n);
}
}
@9rnsr
Copy link
Author

9rnsr commented Jun 5, 2012

変更点は:

  • lambda式を使って短く書く
  • zeroをmainの関数内関数にすることでdelegate_from_functionを省く
  • succはbug8198のせいでネストしたlambda式を書けない。修正Patchを投げる予定投げました
  • zeroやsuccの複雑なシグネチャは省略できない(省くとそもそも型推論ができないので)

@9rnsr
Copy link
Author

9rnsr commented Jun 13, 2012

  • bug8198がfixされたので、コメントアウトしていたより短いコードに変更

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