Add IEnumerable<T>.Shuffled()

Shuffled() wraps Shuffle() and returns a new collection as the result.
Continuation of #18
This commit is contained in:
Oliver Booth 2021-01-16 14:18:17 +00:00
parent 9238459851
commit 0512d7d568
3 changed files with 15 additions and 2 deletions

View File

@ -8,6 +8,19 @@ namespace X10D
/// </summary>
public static class EnumerableExtensions
{
/// <summary>
/// Reorganizes the elements in an enumerable by implementing a Fisher-Yates shuffle, and returns th shuffled result.
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The <see cref="IEnumerable{T}" /> to shuffle.</param>
/// <param name="random">Optional. The <see cref="System.Random" /> instance to use for the shuffling.</param>
public static IReadOnlyCollection<T> Shuffled<T>(this IEnumerable<T> source, System.Random? random = null)
{
var list = new List<T>(source);
list.Shuffle(random);
return list.AsReadOnly();
}
/// <summary>
/// Splits <paramref name="value" /> into chunks of size <paramref name="chunkSize" />.
/// </summary>

View File

@ -60,7 +60,7 @@ namespace X10D
/// </summary>
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The <see cref="IList{T}" /> to shuffle.</param>
/// <param name="random">The <see cref="Random" /> instance.</param>
/// <param name="random">Optional. The <see cref="System.Random" /> instance to use for the shuffling.</param>
public static void Shuffle<T>(this IList<T> source, System.Random? random = null)
{
if (source is null)

View File

@ -336,7 +336,7 @@ namespace X10D
throw new ArgumentNullException(nameof(str));
}
return new string(str.ToCharArray().Shuffle(random).ToArray());
return new string(str.Shuffled(random).ToArray());
}
/// <summary>