Skip to content

Instantly share code, notes, and snippets.

@johannes-riecken
Created October 20, 2018 09:44
Show Gist options
  • Save johannes-riecken/9c61b2300d68af4233e5627741ac4ec9 to your computer and use it in GitHub Desktop.
Save johannes-riecken/9c61b2300d68af4233e5627741ac4ec9 to your computer and use it in GitHub Desktop.
Factorial in pure LINQ
private class Wrap<T>
{
public readonly Func<Wrap<T>, T> It;
public Wrap(Func<Wrap<T>, T> it)
{
It = it;
}
}
public static Func<T, U> Y<T, U>(Func<Func<T, U>, Func<T, U>> f)
{
Func<Wrap<Func<T, U>>, Func<T, U>> g = wx => f(wx.It(wx));
return g(new Wrap<Func<T, U>>(wx => f(y => wx.It(wx)(y))));
}
public static void Main()
{
var query = from n in Enumerable.Range(0, 10)
let computeFact = (Func<Func<int, int>, Func<int, int>>) (f => n1 => n1 == 0 ? 1 : (n1 * f(n1 - 1)))
let fact = Y(computeFact)
select new {X = fact(n)};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment