Replies: 31 comments
-
it would be useful if this were expanded slightly to use more than just IEnumerable<int> sequence = ...
var concated1 = 1 + sequence; // adds 1 to the start of the enumeration
var concated2 = sequence + 1; // adds 1 to the end of the enumeration |
Beta Was this translation helpful? Give feedback.
-
I would prefer this to be solved at the library level instead of the language level. May be Default Interfaces (#52) could support defining operators, and the interface IEnumerable<T> {
static IEnumerable<T> operator + (IEnumerable<T> a, IEnumerable<T> b) => a?.Concat (b) ?? b;
} |
Beta Was this translation helpful? Give feedback.
-
Some more suggestions for list and set operators (including the proposal for completeness):
and their counterparts for updating the original: edit: Also from Python is |
Beta Was this translation helpful? Give feedback.
-
Concatenation of |
Beta Was this translation helpful? Give feedback.
-
Surely it's only quadratic if eager concatenation is performed? If the returned type is some Also (and my eyes I bleeding as I read what I'm typing here as it's paining me to say this 🙃), if |
Beta Was this translation helpful? Give feedback.
-
The result is no different, but I feel calling Now I could see a case where someone were to create a list implementation with a constant-time |
Beta Was this translation helpful? Give feedback.
-
Nevertheless, someone who has tried and failed to use So I don't see how adding this feature makes things worse. It just makes things easier to read. |
Beta Was this translation helpful? Give feedback.
-
Why it's quadratic if concatenation does simply enumerate over both sequences? It's equivalent of IEnumerable<T> Concat<T>(IEnumerable<T> a, IEnumerable<T> b) {
foreach (var i in a) yield return i;
foreach (var i in b) yield return i;
} There are no performance problems. |
Beta Was this translation helpful? Give feedback.
-
It's quadratic for number of composed concats, not number of elements. |
Beta Was this translation helpful? Give feedback.
-
@Opiumtm You don't really need hundreds to notice a difference with Regardless of that it is inconsequential to your proposal. I agree with @MillKaDe, it would make more sense for this to be implemented via default interfaces than as a language change. |
Beta Was this translation helpful? Give feedback.
-
@scalablecory |
Beta Was this translation helpful? Give feedback.
-
The quadratic behavior is when you're concatenating n sequences (and let's call the total number of items in all sequences m). In such cases, I believe the time complexity is O(n2 + m) on .Net Core and O(nm) on .Net Framework (assuming each sequence has the same number of items). So, at least on .Net Framework, I would expect the performance to be bad even when concatenating just tens of sequences. |
Beta Was this translation helpful? Give feedback.
-
@svick |
Beta Was this translation helpful? Give feedback.
-
Regardless, if you need to concat, you need to concat. Saying that |
Beta Was this translation helpful? Give feedback.
-
Related: #378 |
Beta Was this translation helpful? Give feedback.
-
I think the general advice is to just Add all the elements into an ever growing List rather than using Concat over and over again. In either case, this proposal is really not so much a language request. It would first require extension operators, and then the BCL team would have to actually implement. |
Beta Was this translation helpful? Give feedback.
-
@MgSam Agreed (I assumed plain I am curious, why doesn't Concat special-case the parameters by type? I get much better performance for lists with just this alteration: if (first is List<T> list1 && second is List<T> list2)
{
var result = new List<TSource>(list1);
result.AddRange(list2);
return result;
}
// else the existing Enumerable.Concat() |
Beta Was this translation helpful? Give feedback.
-
A typecheck incurs its own costs. Now you're taking that hit for all values that are not lists. Also, you've now made concat non-lazy. And you are causing a memory allocation of size(list1) + size(list2). |
Beta Was this translation helpful? Give feedback.
-
I don't see "it's currently a slow implementation" as a decent argument against making it a language feature. (There are plenty of other reasons, already. 😄) |
Beta Was this translation helpful? Give feedback.
-
This feature may be misleading. For example var result = new[]{1,2,3} + new[]{4,5,6}; I'd rather expect that I think we can use syntax like: var result = [new[]{1,2,3}, new[]{4,5,6} ] similar to F# let result = [| for i in 1 .. 10 -> i * i |] And leave |
Beta Was this translation helpful? Give feedback.
-
@Pzixel Could For that matter, specializing the operator in such a manner could optimize performance for each type. (And I would love to see list/array comprehensions in C#.) |
Beta Was this translation helpful? Give feedback.
-
@bondsbw sometimes users want to concat arrays as a new array, sometime just as |
Beta Was this translation helpful? Give feedback.
-
@Pzixel I can see that. In C#, A separate lazy operator may be warranted. Say |
Beta Was this translation helpful? Give feedback.
-
@bondsbw can't say it's generally an eager operator since it works so for Concatenation of sequences is not really often operation (ask yourself how recently did you use UNION ALL in SQL), however, I'd like to have in language, but it's just a |
Beta Was this translation helpful? Give feedback.
-
I would say it depends on the type of the references. If I have variables that are typed as arrays or lists, I would expect it to alloc new lists or arrays. If I have variables that are typed as IEnumerables, I would expect it to use a wrapper that iterates across the two enumerables. Remember that it's easy to convert the second to the first with |
Beta Was this translation helpful? Give feedback.
-
Last week, using LINQ |
Beta Was this translation helpful? Give feedback.
-
I had used it very extensively for Microsoft Analysis Services dimension SQL view recently. And I had used it for facts SQL view too. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure why Enumerable.Concat has to be quadradic. I would suggest something like this:
|
Beta Was this translation helpful? Give feedback.
-
Won't you be able to do this yourself once Extension Everything roles out? |
Beta Was this translation helpful? Give feedback.
-
I'd suggest to alter the language a bit in such a way that a + b would be handled as follows:
The IEnumerable + IEnumerable would be covered by the items 3 and 4 as soon as the System.Linq namespace is visible, as there is already a suitable Enumerable.Concat() extension method. We would also be able to add the eager overloads even for the platform types like T[] and List, as the extension methods can be contributed by anyone. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Use of
+
operator to concatenate two compatibleIEnumerable<T>
sequences is natural and concatenation of sequences is quite common.So, instead of
allow to write
As custom operator can't be declared for interface (
IEnumerable<T>
in this case), there is no manual way to overload+
for IEnumerables concatenation.Beta Was this translation helpful? Give feedback.
All reactions