dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.05k stars 4.69k forks source link

[API Proposal]: Adding ToSortedSet and ToSortedDictionary extension methods to the Enumerable class #79780

Open AlexRadch opened 1 year ago

AlexRadch commented 1 year ago

Background and motivation

All sets and dictionaries (HashSet, Dictionary, ILookup, ImmutableHashSet, ImmutableSortedSet, ImmutableDictionary, ImmutableSortedDictionary, FrozenSet, FrozenDictionary) have extension methods that convert enumerable to that set/dictionary except SortedSet and SortedDictionary.

I suggest adding ToSortedSet and ToSortedDictionary extension methods to the Enumerable class.

API Proposal

namespace System.Linq;

public static class Enumerable
{
    public static SortedSet<TSource> ToSortedSet<TSource> (this IEnumerable<TSource> source);
    public static SortedSet<TSource> ToSortedSet<TSource> (this IEnumerable<TSource> source,
        IEqualityComparer<TSource>? compare);

    public static SortedDictionary<TKey,TSource> ToSortedDictionary<TSource,TKey> (
        this IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
    public static SortedDictionary<TKey,TSource> ToSortedDictionary<TSource,TKey> (
        this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, IEqualityComparer<TKey>? compare);
    public static SortedDictionary<TKey,TElement> ToSortedDictionary<TSource,TKey,TElement> (
        this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
    public static SortedDictionary<TKey,TElement> ToSortedDictionary<TSource,TKey,TElement> (
        this IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector,
        IEqualityComparer<TKey>? compare);
}

API Usage

The same as ToHashSet and ToDictionary.

Alternative Designs

No response

Risks

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation All sets and dictionaries (HashSet, Dictionary, ILookup, ImmutableHashSet, ImmutableSortedSet, ImmutableDictionary, ImmutableSortedDictionary, FrozenSet, FrozenDictionary) have extension methods that convert enumerable to that set/dictionary except `SortedSet` and `SortedDictionary`. I suggest adding ToSortedSet and ToSortedDictionary API extension methods to the Enumerable class. ### API Proposal ```csharp namespace System.Linq; public static class Enumerable { public static SortedSet ToSortedSet (this IEnumerable source); public static SortedSet ToSortedSet (this IEnumerable source, IEqualityComparer? compare); public static SortedDictionary ToSortedDictionary ( this IEnumerable source, Func keySelector); public static SortedDictionary ToSortedDictionary ( this IEnumerable source, Func keySelector, IEqualityComparer? compare); public static SortedDictionary ToSortedDictionary ( this IEnumerable source, Func keySelector, Func elementSelector); public static SortedDictionary ToSortedDictionary ( this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer? compare); } ``` ### API Usage The same as `ToHashSet` and `ToDictionary`. ### Alternative Designs _No response_ ### Risks _No response_
Author: AlexRadch
Assignees: -
Labels: `api-suggestion`, `area-System.Collections`
Milestone: -
eiriktsarpalis commented 1 year ago

Note that the immutable/frozen extension methods are defined on the collection types themselves rather than Linq. ToHashSet/ToDictionary/ToLookup are exceptions as they serve as accelerator methods for common lookup patterns. ToSortedSet/ToSortedDictionary don't add much functionality-wise other than providing alternative set/dictionary implementations that are typically slower than their hashtable equivalents.

I don't see how these additions would be useful other than use cases specifically looking to construct the particular collection types. Similar to the immutable/frozen extensions, we might consider adding these to the collection types directly, but I'm not sure that it would be valuable for most users.