Skip to content

Instantly share code, notes, and snippets.

@halter73
Last active December 15, 2015 07:49
Show Gist options
  • Save halter73/5225984 to your computer and use it in GitHub Desktop.
Save halter73/5225984 to your computer and use it in GitHub Desktop.
Func<int[], string> toRanges = source => source.Length == 0 ? "" :
String.Join(",", source.Skip(1).Aggregate(Enumerable.Repeat(Tuple.Create(source[0], source[0]), 1),
(combined, i) => combined.Last().Item2 == i - 1 ?
combined.Take(combined.Count() - 1).Concat(new[] { Tuple.Create(combined.Last().Item1, i) }) :
combined.Concat(new[] { Tuple.Create(i, i) })
).Select(range => range.Item1 == range.Item2 ? range.Item1.ToString() : range.Item1 + "-" + range.Item2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment