-1

It's not a question (however see bottom of the post). Just for fun - just I trained with new C# features.
Haskell vs C# 8.0 :)

static class CS8_Tests
{
    public static void Deconstruct<T>(this IEnumerable<T> items, out T head, out IEnumerable<T> tail)
        => (head, tail) = (items.FirstOrDefault(), items.Skip(1));

    /// <summary>
    /// Famous Haskell implementation:
    /// quicksort :: (Ord a) => [a] –> [a]
    /// quicksort[] = []
    /// quicksort(x:xs) =
    /// let smallerSorted = quicksort[a | a <– xs, a <= x]
    ///     biggerSorted  = quicksort[a | a <– xs, a >  x]
    /// in smallerSorted ++ [x] ++ biggerSorted
    /// </summary>
    static IEnumerable<T> quickSort<T>(IEnumerable<T> items) where T : IComparable<T>
        => items switch
        {
            _ when items.Count() == 0 => items,
            (var x, var xs) => quickSort(xs.Where(a => a.CompareTo(x) <= 0))
                              .Append(x)
                              .Concat(quickSort(xs.Where(a => a.CompareTo(x) > 0)))
        };

    static int indexOf<T>(IEnumerable<T> items, T target, int index = 0) where T : IEquatable<T>
        => items switch
        {
            (var h, _) when h.Equals(target) => index,
            (_, var tail) when tail.Count() == 0 => -1,
            (_, var tail) => indexOf(tail, target, index + 1),
        };

    public static void RunTests()
    {
        var items = new List<int> { 2, 5, 7, 5, 1, 4, 3, 1 };
        var sorted_items = quickSort(items).ToList();
        var index = indexOf(items, 1);
    }

    //public static IEnumerable<T> operator +(IEnumerable<T> elms1, IEnumerable<T> elms2)
    //    => elms1.Concat(elms2);   
}

Unfortunately, we cannot define "+" operator for IEnumerable - then this code would be more compact (see commented lines at the bottom of the code).
It is interesting - why? - this may be my question.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Lealan
  • 136
  • 1
  • 1
  • 8
  • 3
    It looks interesting, but I would recommend you post this on stackexchange (codereview) – Abishek Aditya Sep 27 '19 at 07:33
  • Thank you! I didn't know about existence of the "stackexchange". – Lealan Sep 27 '19 at 07:37
  • Usually, if the code is not working you post it to SO, but if you want to improve your working code and are looking for people to make the code better you post it to codereview. Glad I could help – Abishek Aditya Sep 27 '19 at 07:38
  • This would not be appropriate on code review. The simple answer to your question is that you cannot define operators outside of the type definition. It may be possible when the team implements [extension function members](https://github.com/dotnet/csharplang/issues/192). – Jeff Mercado Oct 07 '19 at 21:06
  • If you wrote the `IEnumerable` type and it wasn't an interface, then obviously you could add the overloaded operator yourself. So your question really is asking why you can't extend a type you don't have control over -- i.e. write an extension method for it. See marked duplicate for details. – Peter Duniho Oct 07 '19 at 21:34

1 Answers1

0

The short answer is because you can't define operators on interfaces and C# doesn't support extension operators (yet).

As to why it hasn't been added, the longer answer is because nothing is free. It takes time (and thus money) to design, document, implement, test, release, and support features. My guess is that this one (extension operators) costs more (due to complexity) than other features of similar/higher value.

D Stanley
  • 149,601
  • 11
  • 178
  • 240