Skip to content

Instantly share code, notes, and snippets.

@kuhlenh
Last active August 5, 2016 16:43
Show Gist options
  • Save kuhlenh/d3f9cfbf10b60e13105fee6dd86dc7c0 to your computer and use it in GitHub Desktop.
Save kuhlenh/d3f9cfbf10b60e13105fee6dd86dc7c0 to your computer and use it in GitHub Desktop.
using static System.Console;
static void Main(string[] args)
{
(int sum, int count) Tally(object[] values)
{
var r = (s: 0, c: 0);
foreach (var v in values)
{
switch(v)
{
case int i:
r = (r.s + i, r.c + 1);
break;
case object[] l:
var n = Tally(l);
r = (r.s + n.sum, r.c + n.count);
break;
}
}
return r;
}
object[] numbers = { 0b1, 0b10, 0b100, new object[] { 0b100, 0b1_0000 }, 0b10_0000 };
var t = Tally(numbers);
WriteLine($"Sum: {t.sum}, count: {t.count}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment