mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 03:45:41 +00:00
Merge pull request #51 from oliverbooth/50-ienumerable-for_and_foreach
IEnumerable<T>.For and ForEach
This commit is contained in:
commit
ed27044a3b
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using X10D.Collections;
|
||||||
using X10D.Core;
|
using X10D.Core;
|
||||||
|
|
||||||
namespace X10D.Tests.Core;
|
namespace X10D.Tests.Core;
|
||||||
@ -17,4 +18,67 @@ public class EnumerableTests
|
|||||||
Assert.IsTrue(array.Count() == 1);
|
Assert.IsTrue(array.Count() == 1);
|
||||||
Assert.AreEqual(o, array.ElementAt(0));
|
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; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
<PackageIconUrl/>
|
<PackageIconUrl/>
|
||||||
<PackageTags>dotnet extension-methods</PackageTags>
|
<PackageTags>dotnet extension-methods</PackageTags>
|
||||||
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
|
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
|
||||||
<VersionPrefix>3.0.0</VersionPrefix>
|
<VersionPrefix>3.1.0</VersionPrefix>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
|
@ -7,6 +7,74 @@ namespace X10D.Collections;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class EnumerableExtensions
|
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>
|
/// <summary>
|
||||||
/// Reorganizes the elements in an enumerable by implementing a Fisher-Yates shuffle, and returns th shuffled result.
|
/// Reorganizes the elements in an enumerable by implementing a Fisher-Yates shuffle, and returns th shuffled result.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user