Created
July 12, 2016 18:05
-
-
Save MikeMKH/f5e2bf37b384308d25d54793d9e4f752 to your computer and use it in GitHub Desktop.
C# 7 Local Function example with tuples in a tail call
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
using System; | |
// Visual Studio 15 Preview 3 | |
// Conditional compilation symbols: __DEMO__ ,__DEMO_EXPERIMENTAL__ | |
// Manually added ValueTuples.cs to project https://raw.githubusercontent.com/dotnet/roslyn/future/docs/features/ValueTuples.cs | |
namespace TupleExamples | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
long Factorial(int x) | |
{ | |
long Aux((int acc, int n) a) | |
{ | |
if (a.n <= 1) return a.acc; | |
var r = (a.acc * a.n, a.n - 1); | |
return Aux(r); | |
} | |
(int, int) t = (1, x); | |
return Aux(t); | |
} | |
Console.WriteLine($"1! = {Factorial(1)}"); | |
Console.WriteLine($"5! = {Factorial(5)}"); | |
Console.WriteLine($"20! = {Factorial(20)}"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same code as https://gist.github.com/MikeMKH/7488b5ce907bb58912f7d4d476ebdf48 but playing with defining the Tuples types.