1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

Add IEnumerable<T>.For and ForEach (#50)

This commit is contained in:
Oliver Booth 2022-05-01 22:25:37 +01:00
parent be845bccf2
commit 5d3a82a33a
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 132 additions and 0 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Collections;
using X10D.Core;
namespace X10D.Tests.Core;
@ -17,4 +18,67 @@ public class EnumerableTests
Assert.IsTrue(array.Count() == 1);
Assert.AreEqual(o, array.ElementAt(0));
}
[TestMethod]
public void For_ShouldTransform_GivenTransformationDelegate()
{
var oneToTen = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var multipliedByIndex = new[] {0, 2, 6, 12, 20, 30, 42, 56, 72, 90};
IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
source.For((i, o) => o.Value *= i);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(multipliedByIndex, values.ToArray());
}
[TestMethod]
public void For_ShouldThrow_GivenNullSource()
{
IEnumerable<object>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.For((_, _) => { }));
}
[TestMethod]
public void For_ShouldThrow_GivenNullAction()
{
IEnumerable<object> source = ArraySegment<object>.Empty;
Assert.ThrowsException<ArgumentNullException>(() => source.For(null!));
}
[TestMethod]
public void ForEach_ShouldTransform_GivenTransformationDelegate()
{
var oneToTen = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var oneToTenDoubled = new[] {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
IEnumerable<DummyClass> source = oneToTen.Select(i => new DummyClass {Value = i}).ToArray();
IEnumerable<int> values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTen, values.ToArray());
source.ForEach(o => o.Value *= 2);
values = source.Select(o => o.Value);
CollectionAssert.AreEqual(oneToTenDoubled, values.ToArray());
}
[TestMethod]
public void ForEach_ShouldThrow_GivenNullSource()
{
IEnumerable<object>? source = null;
Assert.ThrowsException<ArgumentNullException>(() => source!.ForEach(_ => { }));
}
[TestMethod]
public void ForEach_ShouldThrow_GivenNullAction()
{
IEnumerable<object> source = ArraySegment<object>.Empty;
Assert.ThrowsException<ArgumentNullException>(() => source.ForEach(null!));
}
private class DummyClass
{
public int Value { get; set; }
}
}

View File

@ -7,6 +7,74 @@ namespace X10D.Collections;
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// Performs the specified action on each element of the <see cref="IEnumerable{T}" />.
/// </summary>
/// <param name="source">
/// The <see cref="IEnumerable{T}" /> whose elements on which to perform <paramref name="action" />.
/// </param>
/// <param name="action">
/// The <see cref="Action{T, T}" /> delegate to perform on each element of the <see cref="IEnumerable{T}" />. The
/// <see cref="int" /> argument passed to this delegate represents the index.
/// </param>
/// <typeparam name="T">The type of the elements in <paramref name="source" />.</typeparam>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="source" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="action" /> is <see langword="null" />.</para>
/// </exception>
public static void For<T>(this IEnumerable<T> source, Action<int, T> action)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
var index = 0;
foreach (T item in source)
{
action(index++, item);
}
}
/// <summary>
/// Performs the specified action on each element of the <see cref="IEnumerable{T}" />.
/// </summary>
/// <param name="source">
/// The <see cref="IEnumerable{T}" /> whose elements on which to perform <paramref name="action" />.
/// </param>
/// <param name="action">
/// The <see cref="Action{T}" /> delegate to perform on each element of the <see cref="IEnumerable{T}" />.
/// </param>
/// <typeparam name="T">The type of the elements in <paramref name="source" />.</typeparam>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="source" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="action" /> is <see langword="null" />.</para>
/// </exception>
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
foreach (T item in source)
{
action(item);
}
}
/// <summary>
/// Reorganizes the elements in an enumerable by implementing a Fisher-Yates shuffle, and returns th shuffled result.
/// </summary>