Skip to content

Commit

Permalink
EnumerableExtensions.GroupWhile - remove useless overload
Browse files Browse the repository at this point in the history
  • Loading branch information
ig-sinicyn committed Dec 19, 2016
1 parent 6bbb236 commit 6925828
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 58 deletions.
35 changes: 0 additions & 35 deletions Main/src/Collections/Enumerable/EnumerableExtensions.GroupWhile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,6 @@ private static IEnumerable<IGrouping<TKey, TItem>> GroupWhileCore<T, TItem, TKey
#endregion

#region While(true)
/// <summary>Groups items in the sequence while they have same grouping key.</summary>
/// <typeparam name="T">Type of items in sequence</typeparam>
/// <param name="source">The source.</param>
/// <param name="predicate">Gropung predicate.</param>
/// <returns>Grouped items.</returns>
public static IEnumerable<T[]> GroupWhile<T>(
[NotNull] this IEnumerable<T> source,
[NotNull] Func<T, bool> predicate)
{
Code.NotNull(source, nameof(source));
Code.NotNull(predicate, nameof(predicate));
return GroupWhileCore(source, predicate);
}

private static IEnumerable<T[]> GroupWhileCore<T>(
IEnumerable<T> source, Func<T, bool> predicate)
{
var groupingList = new List<T>();

foreach (var item in source)
{
if (groupingList.Count > 0 && !predicate(item))
{
yield return groupingList.ToArray();
groupingList.Clear();
}
groupingList.Add(item);
}

if (groupingList.Count > 0)
{
yield return groupingList.ToArray();
}
}

/// <summary>Groups items in the sequence while they have same grouping key.</summary>
/// <typeparam name="T">Type of items in sequence</typeparam>
/// <param name="source">The source.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,13 @@ public void GroupWhileToString(string input, string expected)

[TestCase("1", 2, "1")]
[TestCase("1,2", 2, "1,2")]
[TestCase("1,2,3,5,8,9,10,123,1,2,3,4", 2, "1,2,3,5|8,9,10|123|1,2,3,4")]
[TestCase("1,2,3,5,8,9,10,123,1,2,3,4,8,12", 2, "1,2,3,5|8,9,10|123|1,2,3,4|8|12")]
public void GroupWhileDelta(string input, int delta, string expected)
{
var data = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var grouping = data.GroupWhile((a, b) => Math.Abs(a - b) <= delta);
var result = grouping.Select(g => g.Join(",")).Join("|");
Assert.AreEqual(result, expected);
}

[TestCase("1", "1")]
[TestCase("1,1", "1,1")]
[TestCase("1,2,3,5,8,9,10,123,1,2,3,4", "1|2,3,5|8,9|10,123,1|2,3|4")]
public void GroupWhileOdd(string input, string expected)
{
var data = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var grouping = data.GroupWhile(a => a % 2 == 1);
var result = grouping.Select(g => g.Join(",")).Join("|");
Assert.AreEqual(result, expected);
}

[TestCase("1", "1")]
[TestCase("1,1", "1,1")]
[TestCase("1,2,3,5,10,11,12,1,2,3,5,6,10", "1,2,3,5|10|11|12,1,2,3,5,6|10")]
public void GroupWhileLess10(string input, string expected)
{
var data = input.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
var grouping = data.GroupWhile(a => a < 10);
var result = grouping.Select(g => g.Join(",")).Join("|");
Assert.AreEqual(result, expected);
}
}
}

0 comments on commit 6925828

Please sign in to comment.