Created
November 11, 2011 18:10
-
-
Save jjasonclark/1358725 to your computer and use it in GitHub Desktop.
Find ranges in collection of ints
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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Program | |
{ | |
internal class RangeFinder | |
{ | |
public static IEnumerable<T> Create<T>(IEnumerable<int> items, Func<int, int, T> rangeCreator) | |
{ | |
return items | |
.OrderBy(v => v) | |
.Select((v, i) => new { value = v, group = v - i }) | |
.GroupBy(b => b.group, b => b.value, (key,group) => rangeCreator(group.First(), group.Last())); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment