Created
March 22, 2011 03:52
-
-
Save masaedw/880742 to your computer and use it in GitHub Desktop.
Slice & Split
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
| static class ExtentionMethods | |
| { | |
| /// <summary> | |
| /// sequenceをn要素のリストに分割する。 | |
| /// sequenceの要素数がnで割り切れない場合は最後のリストの要素数はn個より少なくなる。 | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="list"></param> | |
| /// <param name="n"></param> | |
| /// <returns></returns> | |
| static public IEnumerable<IEnumerable<T>> Slice<T>(this IEnumerable<T> sequence, int n) | |
| { | |
| var i = sequence.Split(n); | |
| while (i.Item1.Any()) | |
| { | |
| yield return i.Item1; | |
| i = i.Item2.Split(n); | |
| } | |
| } | |
| /// <summary> | |
| /// sequenceに対してTakeとSkipを行い、それぞれの結果を返す。 | |
| /// </summary> | |
| /// <typeparam name="T"></typeparam> | |
| /// <param name="sequence"></param> | |
| /// <param name="n"></param> | |
| /// <returns></returns> | |
| static public Tuple<IEnumerable<T>, IEnumerable<T>> Split<T>(this IEnumerable<T> sequence, int n) | |
| { | |
| return Tuple.Create(sequence.Take(n), sequence.Skip(n)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment