Last active
August 5, 2016 16:43
-
-
Save kuhlenh/d3f9cfbf10b60e13105fee6dd86dc7c0 to your computer and use it in GitHub Desktop.
This file contains 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 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