using System.Security.Cryptography; namespace Nadeko.Common; public static class EnumerableExtensions { /// /// Concatenates the members of a collection, using the specified separator between each member. /// /// Collection to join /// /// The character to use as a separator. separator is included in the returned string only if /// values has more than one element. /// /// Optional transformation to apply to each element before concatenation. /// The type of the members of values. /// /// A string that consists of the members of values delimited by the separator character. -or- Empty if values has /// no elements. /// public static string Join(this IEnumerable data, char separator, Func? func = null) => string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty))); /// /// Concatenates the members of a collection, using the specified separator between each member. /// /// Collection to join /// /// The string to use as a separator.separator is included in the returned string only if values /// has more than one element. /// /// Optional transformation to apply to each element before concatenation. /// The type of the members of values. /// /// A string that consists of the members of values delimited by the separator character. -or- Empty if values has /// no elements. /// public static string Join(this IEnumerable data, string separator, Func? func = null) => string.Join(separator, data.Select(func ?? (x => x?.ToString() ?? string.Empty))); /// /// Randomize element order by performing the Fisher-Yates shuffle /// /// Item type /// Items to shuffle public static IReadOnlyList Shuffle(this IEnumerable items) { var list = items.ToArray(); var n = list.Length; while (n-- > 1) { var k = RandomNumberGenerator.GetInt32(n); (list[k], list[n]) = (list[n], list[k]); } return list; } /// /// Initializes a new instance of the class /// that contains elements copied from the specified /// has the default concurrency level, has the default initial capacity, /// and uses the default comparer for the key type. /// /// /// The whose elements are copied to the new /// . /// /// A new instance of the class public static ConcurrentDictionary ToConcurrent( this IEnumerable> dict) where TKey : notnull => new(dict); public static IndexedCollection ToIndexed(this IEnumerable enumerable) where T : class, IIndexed => new(enumerable); /// /// Creates a task that will complete when all of the objects in an enumerable /// collection have completed /// /// The tasks to wait on for completion. /// The type of the completed task. /// A task that represents the completion of all of the supplied tasks. public static Task WhenAll(this IEnumerable> tasks) => Task.WhenAll(tasks); /// /// Creates a task that will complete when all of the objects in an enumerable /// collection have completed /// /// The tasks to wait on for completion. /// A task that represents the completion of all of the supplied tasks. public static Task WhenAll(this IEnumerable tasks) => Task.WhenAll(tasks); }