-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collection extensions upd #2649
Collection extensions upd #2649
Conversation
I think there is also an issue with inconsistent null-checks - some methods throw exceptions (like Shuffle), some return default values (like Random). |
…ling part, apparently
@@ -41,7 +41,7 @@ public static IEnumerable<T> RemoveSpecified<T>(this IEnumerable<T> enumerable, | |||
/// <typeparam name="T">Type of <see cref="IEnumerable{T}"/> elements.</typeparam> | |||
/// <returns>A random item from the <see cref="IEnumerable{T}"/>.</returns> | |||
public static T Random<T>(this IEnumerable<T> enumerable) => | |||
(enumerable as T[] ?? enumerable?.ToArray()) is { Length: > 0 } arr ? arr[UnityEngine.Random.Range(0, arr.Length)] : default; | |||
(enumerable as IList<T> ?? enumerable?.ToArray()) is { Count: > 0 } arr ? arr[UnityEngine.Random.Range(0, arr.Count)] : default; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is to avoid calling .ToArray() if enumerable is a generic List<>. Arrays implement IList<> as well -
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/arrays#1723-arrays-and-the-generic-collection-interfaces
Maybe we should add an "Instance" field to Plugin<> class instead of making all those GetPlugin() methods ? |
Isn't this outside the scope of the PR? |
…collection-extensions-upd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a Nw extension (ShuffleList)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this
Okay. Now you need to target dev branch (read exiled announcements if any questions) |
# Conflicts: # Exiled.API/Extensions/CommonExtensions.cs
Shuffle<T>(this IEnumerable<T> enumerable, int iterations = 1)
- added documentation for thrown exceptions & added an actual shuffle instead of infinite recursive callAddRange<T>(this IEnumerable<T> enumerable, IEnumerable<T> collection)
- before was callingAddItem()
in a loop. (multipleConcat()
s internaly). It wouldnt even work becauseAddItem()
is a pure method. Now using oneConcat()
.AddRange<T>(this T[] array, IEnumerable<T> collection)
- same changes.AddRange<T>(this HashSet<T> hashset, IEnumerable<T> collection)
- before was callingAdd()
in a loop. Now usingUnionWith()
.