mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:25:43 +00:00
✨ Add IEnumerable<T>.Shuffled()
Shuffled() wraps Shuffle() and returns a new collection as the result. Continuation of #18
This commit is contained in:
parent
9238459851
commit
0512d7d568
@ -8,6 +8,19 @@ namespace X10D
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class EnumerableExtensions
|
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>
|
/// <summary>
|
||||||
/// Splits <paramref name="value" /> into chunks of size <paramref name="chunkSize" />.
|
/// Splits <paramref name="value" /> into chunks of size <paramref name="chunkSize" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -60,7 +60,7 @@ namespace X10D
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The element type.</typeparam>
|
/// <typeparam name="T">The element type.</typeparam>
|
||||||
/// <param name="source">The <see cref="IList{T}" /> to shuffle.</param>
|
/// <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)
|
public static void Shuffle<T>(this IList<T> source, System.Random? random = null)
|
||||||
{
|
{
|
||||||
if (source is null)
|
if (source is null)
|
||||||
|
@ -336,7 +336,7 @@ namespace X10D
|
|||||||
throw new ArgumentNullException(nameof(str));
|
throw new ArgumentNullException(nameof(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new string(str.ToCharArray().Shuffle(random).ToArray());
|
return new string(str.Shuffled(random).ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user