Add Product extension method

Computes the element-wise product, optionally with a transformation delegate
This commit is contained in:
Oliver Booth 2022-04-20 14:07:16 +01:00
parent 20ea3e46eb
commit 3c60340bde
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
20 changed files with 1341 additions and 3 deletions

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow((byte)0, (byte)1)]
[DataRow((byte)2, (byte)3, (byte)4)]
[DataRow((byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9, (byte)10)] // 10!
public void ProductByte_Fixed(params byte[] source)
{
byte expected = 1;
foreach (byte item in source)
{
expected *= item;
}
byte actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow((byte)0, (byte)1)]
[DataRow((byte)2, (byte)3, (byte)4)]
[DataRow((byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9, (byte)10)] // 10!
public void ProductByte_Transformed_Fixed(params byte[] source)
{
byte expected = 1;
foreach (byte item in source)
{
expected *= (byte)(item * 2);
}
byte actual = source.Product(n => (byte)(n * 2));
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductByte_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new byte[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (byte)random.Next(1, 100);
}
byte expected = 1;
foreach (byte item in source)
{
expected *= item;
}
byte actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductByte_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new byte[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (byte)random.Next(1, 100);
}
byte expected = 1;
foreach (byte item in source)
{
expected *= (byte)(item * 2);
}
byte actual = source.Product(n => (byte)(n * 2));
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,144 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
// the Fixed methods in these tests can't use DataRow attribute because the use of a decimal literal compiles to an
// instantiation of the decimal struct, which is not a compile-time constant expression.
// thanks. I fucking hate it.
[TestMethod]
public void ProductDecimal_Fixed()
{
{
var source = new[] {0m, 1m};
var expected = 1m;
foreach (decimal item in source)
{
expected *= item;
}
decimal actual = source.Product();
Assert.AreEqual(expected, actual);
}
{
var source = new[] {2m, 3m, 4m};
var expected = 1m;
foreach (decimal item in source)
{
expected *= item;
}
decimal actual = source.Product();
Assert.AreEqual(expected, actual);
}
{
var source = new[] {1m, 2m, 3m, 4m, 5m, 6m, 7m, 8m, 9m, 10m};
var expected = 1m;
foreach (decimal item in source)
{
expected *= item;
}
decimal actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductDecimal_Transformed_Fixed()
{
{
var source = new[] {0m, 1m};
var expected = 1m;
foreach (decimal item in source)
{
expected *= item * 2;
}
decimal actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
{
var source = new[] {2m, 3m, 4m};
var expected = 1m;
foreach (decimal item in source)
{
expected *= item * 2;
}
decimal actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
{
var source = new[] {1m, 2m, 3m, 4m, 5m, 6m, 7m, 8m, 9m, 10m};
var expected = 1m;
foreach (decimal item in source)
{
expected *= item * 2;
}
decimal actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductDecimal_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new decimal[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (decimal)random.NextDouble();
}
var expected = 1m;
foreach (decimal item in source)
{
expected *= item;
}
decimal actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductDecimal_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new decimal[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (decimal)random.NextDouble();
}
var expected = 1m;
foreach (decimal item in source)
{
expected *= item * 2;
}
decimal actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow(0.0, 1.0)]
[DataRow(2.0, 3.0, 4.0)]
[DataRow(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)] // 10!
public void ProductDouble_Fixed(params double[] source)
{
var expected = 1.0;
foreach (double item in source)
{
expected *= item;
}
double actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow(0.0, 1.0)]
[DataRow(2.0, 3.0, 4.0)]
[DataRow(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)] // 10!
public void ProductDouble_Transformed_Fixed(params double[] source)
{
var expected = 1.0;
foreach (double item in source)
{
expected *= item * 2;
}
double actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductDouble_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new double[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = random.NextDouble();
}
var expected = 1.0;
foreach (double item in source)
{
expected *= item;
}
double actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductDouble_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new double[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = random.NextDouble();
}
var expected = 1.0;
foreach (double item in source)
{
expected *= item * 2;
}
double actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow((short)0, (short)1)]
[DataRow((short)2, (short)3, (short)4)]
[DataRow((short)1, (short)2, (short)3, (short)4, (short)5, (short)6, (short)7, (short)8, (short)9, (short)10)] // 10!
public void ProductInt16_Fixed(params short[] source)
{
short expected = 1;
foreach (short item in source)
{
expected *= item;
}
short actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow((short)0, (short)1)]
[DataRow((short)2, (short)3, (short)4)]
[DataRow((short)1, (short)2, (short)3, (short)4, (short)5, (short)6, (short)7, (short)8, (short)9, (short)10)] // 10!
public void ProductInt16_Transformed_Fixed(params short[] source)
{
short expected = 1;
foreach (short item in source)
{
expected *= (short)(item * 2);
}
short actual = source.Product(n => (short)(n * 2));
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductInt16_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new short[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (short)random.Next(1, 100);
}
short expected = 1;
foreach (short item in source)
{
expected *= item;
}
short actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductInt16_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new short[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (short)random.Next(1, 100);
}
short expected = 1;
foreach (short item in source)
{
expected *= (short)(item * 2);
}
short actual = source.Product(n => (short)(n * 2));
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow(0, 1)]
[DataRow(2, 3, 4)]
[DataRow(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] // 10!
public void ProductInt32_Fixed(params int[] source)
{
var expected = 1;
foreach (int item in source)
{
expected *= item;
}
int actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow(0, 1)]
[DataRow(2, 3, 4)]
[DataRow(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)] // 10!
public void ProductInt32_Transformed_Fixed(params int[] source)
{
var expected = 1;
foreach (int item in source)
{
expected *= item * 2;
}
int actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductInt32_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new int[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = random.Next(1, 100);
}
var expected = 1;
foreach (int item in source)
{
expected *= item;
}
int actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductInt32_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new int[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = random.Next(1, 100);
}
var expected = 1;
foreach (int item in source)
{
expected *= item * 2;
}
int actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow(0L, 1L)]
[DataRow(2L, 3L, 4L)]
[DataRow(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)] // 10!
public void ProductInt64_Fixed(params long[] source)
{
var expected = 1L;
foreach (long item in source)
{
expected *= item;
}
long actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow(0L, 1L)]
[DataRow(2L, 3L, 4L)]
[DataRow(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L)] // 10!
public void ProductInt64_Transformed_Fixed(params long[] source)
{
var expected = 1L;
foreach (long item in source)
{
expected *= item * 2;
}
long actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductInt64_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new long[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = random.NextInt64(1, 100);
}
var expected = 1L;
foreach (long item in source)
{
expected *= item;
}
long actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductInt64_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new long[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = random.NextInt64(1, 100);
}
var expected = 1L;
foreach (long item in source)
{
expected *= item * 2;
}
long actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow((sbyte)0, (sbyte)1)]
[DataRow((sbyte)2, (sbyte)3, (sbyte)4)]
[DataRow((sbyte)1, (sbyte)2, (sbyte)3, (sbyte)4, (sbyte)5, (sbyte)6, (sbyte)7, (sbyte)8, (sbyte)9, (sbyte)10)] // 10!
public void ProductSByte_Fixed(params sbyte[] source)
{
sbyte expected = 1;
foreach (sbyte item in source)
{
expected *= item;
}
sbyte actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow((sbyte)0, (sbyte)1)]
[DataRow((sbyte)2, (sbyte)3, (sbyte)4)]
[DataRow((sbyte)1, (sbyte)2, (sbyte)3, (sbyte)4, (sbyte)5, (sbyte)6, (sbyte)7, (sbyte)8, (sbyte)9, (sbyte)10)] // 10!
public void ProductSByte_Transformed_Fixed(params sbyte[] source)
{
sbyte expected = 1;
foreach (sbyte item in source)
{
expected *= (sbyte)(item * 2);
}
sbyte actual = source.Product(n => (sbyte)(n * 2));
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductSByte_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new sbyte[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (sbyte)random.Next(1, 100);
}
sbyte expected = 1;
foreach (sbyte item in source)
{
expected *= item;
}
sbyte actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductSByte_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new sbyte[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (sbyte)random.Next(1, 100);
}
sbyte expected = 1;
foreach (sbyte item in source)
{
expected *= (sbyte)(item * 2);
}
sbyte actual = source.Product(n => (sbyte)(n * 2));
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow(0f, 1f)]
[DataRow(2f, 3f, 4f)]
[DataRow(1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f)] // 10!
public void ProductSingle_Fixed(params float[] source)
{
var expected = 1f;
foreach (float item in source)
{
expected *= item;
}
float actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow(0f, 1f)]
[DataRow(2f, 3f, 4f)]
[DataRow(1f, 2f, 3f, 4f, 5f, 6f, 7f, 8f, 9f, 10f)] // 10!
public void ProductSingle_Transformed_Fixed(params float[] source)
{
var expected = 1f;
foreach (float item in source)
{
expected *= item * 2;
}
float actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductSingle_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new float[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (float)random.NextDouble();
}
var expected = 1f;
foreach (float item in source)
{
expected *= item;
}
float actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductSingle_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new float[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (float)random.NextDouble();
}
var expected = 1f;
foreach (float item in source)
{
expected *= item * 2;
}
float actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow((ushort)0, (ushort)1)]
[DataRow((ushort)2, (ushort)3, (ushort)4)]
[DataRow((ushort)1, (ushort)2, (ushort)3, (ushort)4, (ushort)5, (ushort)6, (ushort)7, (ushort)8, (ushort)9, (ushort)10)] // 10!
public void ProductUInt16_Fixed(params ushort[] source)
{
ushort expected = 1;
foreach (ushort item in source)
{
expected *= item;
}
ushort actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow((ushort)0, (ushort)1)]
[DataRow((ushort)2, (ushort)3, (ushort)4)]
[DataRow((ushort)1, (ushort)2, (ushort)3, (ushort)4, (ushort)5, (ushort)6, (ushort)7, (ushort)8, (ushort)9, (ushort)10)] // 10!
public void ProductUInt16_Transformed_Fixed(params ushort[] source)
{
ushort expected = 1;
foreach (ushort item in source)
{
expected *= (ushort)(item * 2);
}
ushort actual = source.Product(n => (ushort)(n * 2));
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductUInt16_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new ushort[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (ushort)random.Next(1, 100);
}
ushort expected = 1;
foreach (ushort item in source)
{
expected *= item;
}
ushort actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductUInt16_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new ushort[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (ushort)random.Next(1, 100);
}
ushort expected = 1;
foreach (ushort item in source)
{
expected *= (ushort)(item * 2);
}
ushort actual = source.Product(n => (ushort)(n * 2));
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow(0u, 1u)]
[DataRow(2u, 3u, 4u)]
[DataRow(1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u)] // 10!
public void ProductUInt32_Fixed(params uint[] source)
{
var expected = 1u;
foreach (uint item in source)
{
expected *= item;
}
uint actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow(0u, 1u)]
[DataRow(2u, 3u, 4u)]
[DataRow(1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u)] // 10!
public void ProductUInt32_Transformed_Fixed(params uint[] source)
{
var expected = 1u;
foreach (uint item in source)
{
expected *= item * 2;
}
uint actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductUInt32_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new uint[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (uint) random.Next(1, 100);
}
var expected = 1u;
foreach (uint item in source)
{
expected *= item;
}
uint actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductUInt32_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new uint[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (uint) random.Next(1, 100);
}
var expected = 1u;
foreach (uint item in source)
{
expected *= item * 2;
}
uint actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,90 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
public partial class EnumerableTests
{
[TestMethod]
[DataRow(0UL, 1UL)]
[DataRow(2UL, 3UL, 4UL)]
[DataRow(1UL, 2UL, 3UL, 4UL, 5UL, 6UL, 7UL, 8UL, 9UL, 10UL)] // 10!
public void ProductUInt64_Fixed(params ulong[] source)
{
var expected = 1UL;
foreach (ulong item in source)
{
expected *= item;
}
ulong actual = source.Product();
Assert.AreEqual(expected, actual);
}
[TestMethod]
[DataRow(0UL, 1UL)]
[DataRow(2UL, 3UL, 4UL)]
[DataRow(1UL, 2UL, 3UL, 4UL, 5UL, 6UL, 7UL, 8UL, 9UL, 10UL)] // 10!
public void ProductUInt64_Transformed_Fixed(params ulong[] source)
{
var expected = 1UL;
foreach (ulong item in source)
{
expected *= item * 2;
}
ulong actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void ProductUInt64_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new ulong[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (ulong) random.NextInt64(1, 100);
}
var expected = 1UL;
foreach (ulong item in source)
{
expected *= item;
}
ulong actual = source.Product();
Assert.AreEqual(expected, actual);
}
}
[TestMethod]
public void ProductUInt64_Transformed_Random1000()
{
var random = new Random();
for (var i = 0; i < 1000; i++)
{
var source = new ulong[10];
for (var j = 0; j < source.Length; j++)
{
source[j] = (ulong) random.NextInt64(1, 100);
}
var expected = 1UL;
foreach (ulong item in source)
{
expected *= item * 2;
}
ulong actual = source.Product(n => n * 2);
Assert.AreEqual(expected, actual);
}
}
}

View File

@ -0,0 +1,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace X10D.Tests.Core;
[TestClass]
public partial class EnumerableTests
{
}

View File

@ -0,0 +1,51 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="byte" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="byte" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static byte Product(this IEnumerable<byte> source)
{
return source.Aggregate((byte)1, (current, value) => (byte) (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="sbyte" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="sbyte" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
[CLSCompliant(false)]
public static sbyte Product(this IEnumerable<sbyte> source)
{
return source.Aggregate((sbyte)1, (current, value) => (sbyte) (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="byte" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static byte Product<TSource>(this IEnumerable<TSource> source, Func<TSource, byte> selector)
{
return source.Select(selector).Product();
}
/// <summary>
/// Computes the product of a sequence of <see cref="sbyte" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
[CLSCompliant(false)]
public static sbyte Product<TSource>(this IEnumerable<TSource> source, Func<TSource, sbyte> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -0,0 +1,27 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="decimal" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="decimal" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static decimal Product(this IEnumerable<decimal> source)
{
return source.Aggregate(1m, (current, value) => (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="decimal" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static decimal Product<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -0,0 +1,27 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="double" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="double" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static double Product(this IEnumerable<double> source)
{
return source.Aggregate(1.0, (current, value) => (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="double" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static double Product<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -0,0 +1,51 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="short" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="short" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static short Product(this IEnumerable<short> source)
{
return source.Aggregate((short)1, (current, value) => (short) (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="ushort" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="ushort" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
[CLSCompliant(false)]
public static ushort Product(this IEnumerable<ushort> source)
{
return source.Aggregate((ushort)1, (current, value) => (ushort) (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="short" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static short Product<TSource>(this IEnumerable<TSource> source, Func<TSource, short> selector)
{
return source.Select(selector).Product();
}
/// <summary>
/// Computes the product of a sequence of <see cref="ushort" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
[CLSCompliant(false)]
public static ushort Product<TSource>(this IEnumerable<TSource> source, Func<TSource, ushort> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -0,0 +1,52 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="int" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="int" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static int Product(this IEnumerable<int> source)
{
return source.Aggregate(1, (current, value) => current * value);
}
/// <summary>
/// Computes the product of a sequence of <see cref="uint" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="uint" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
[CLSCompliant(false)]
public static uint Product(this IEnumerable<uint> source)
{
return source.Aggregate(1u, (current, value) => current * value);
}
/// <summary>
/// Computes the product of a sequence of <see cref="int" /> values that are obtained by invoking a transform function on
/// each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static int Product<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
{
return source.Select(selector).Product();
}
/// <summary>
/// Computes the product of a sequence of <see cref="uint" /> values that are obtained by invoking a transform function on
/// each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
[CLSCompliant(false)]
public static uint Product<TSource>(this IEnumerable<TSource> source, Func<TSource, uint> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -0,0 +1,51 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="long" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="long" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static long Product(this IEnumerable<long> source)
{
return source.Aggregate(1L, (current, value) => current * value);
}
/// <summary>
/// Computes the product of a sequence of <see cref="ulong" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="ulong" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
[CLSCompliant(false)]
public static ulong Product(this IEnumerable<ulong> source)
{
return source.Aggregate(1UL, (current, value) => current * value);
}
/// <summary>
/// Computes the product of a sequence of <see cref="long" /> values that are obtained by invoking a transform function on
/// each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static long Product<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector)
{
return source.Select(selector).Product();
}
/// <summary>
/// Computes the product of a sequence of <see cref="ulong" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
[CLSCompliant(false)]
public static ulong Product<TSource>(this IEnumerable<TSource> source, Func<TSource, ulong> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -0,0 +1,27 @@
namespace X10D;
public static partial class EnumerableExtensions
{
/// <summary>
/// Computes the product of a sequence of <see cref="float" /> values.
/// </summary>
/// <param name="source">A sequence of <see cref="float" /> values that are used to calculate the product.</param>
/// <returns>The product the values in the sequence.</returns>
public static float Product(this IEnumerable<float> source)
{
return source.Aggregate(1f, (current, value) => (current * value));
}
/// <summary>
/// Computes the product of a sequence of <see cref="float" /> values that are obtained by invoking a transform function
/// on each element of the input sequence.
/// </summary>
/// <param name="source">A sequence of values that are used to calculate a product.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>The product of the projected values.</returns>
public static float Product<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector)
{
return source.Select(selector).Product();
}
}

View File

@ -3,7 +3,7 @@
/// <summary>
/// Extension methods for <see cref="IEnumerable{T}" />.
/// </summary>
public static class EnumerableExtensions
public static partial class EnumerableExtensions
{
/// <summary>
/// Reorganizes the elements in an enumerable by implementing a Fisher-Yates shuffle, and returns th shuffled result.