Skip to content

Instantly share code, notes, and snippets.

@takeshik
Created October 21, 2012 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takeshik/3928033 to your computer and use it in GitHub Desktop.
Save takeshik/3928033 to your computer and use it in GitHub Desktop.
void Main()
{
Make.Fix<int>(f => n => n < 2 ? 1 : f(n - 1) * n)(5).Dump(); // 120
}
public class Make
{
public static Func<T, T> Fix<T>(Func<Func<T, T>, Func<T, T>> func) { return Fun<T>.Fix(func); }
public static Func<T, T> Fix<T>(Func<Func<T, T>, Func<T, T>> func, T dummy) { return Fun<T>.Fix(func); }
private static class Fun<T>
{
private delegate U SelfApplicable<U>(SelfApplicable<U> self);
private static SelfApplicable<Func<Func<Func<T, T>, Func<T, T>>, Func<T, T>>> Y = y => f => x => f(y(y)(f))(x);
public static Func<Func<Func<T, T>, Func<T, T>>, Func<T, T>> Fix = Y(Y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment