[Proposal]: linq `where` with remove
For ex. we have code:
var list =new List<string>(){
"x",
"x1",
"x2",
"x3",
"y",
"y1",
"y2",
"y3",
"y4",
"z",
"z1",
"z2"
};
And we want to select new list of lists like:
[x:[x1,x2,x3], y:[y1,y2,y3,y4], z:[z1,z2]]
This can be done for ex. with code:
var listOfList=
// here we selecting root elements [x,y,z]
list.Where(x=>x.Length==1)
// here foreach root element we selecting sublist
.Select(x=>
new KeyValuePair<string,List<string>>(x, list.Where(y=>y.IndexOf(x)==0)
But when we selecting sublists, for each root element we call method:
list.Where(y=>y.IndexOf(x)==0
This peace of code could be optimized if we'd have in linq method for ex. like WhereWithRemove, which would select needed elements from the list and then remove them.
This method can be used like:
var list = new List<int>(){1, 2, 3, 4, 5,6,7};
list.Where(x=>x>3 && x<7).ToList(); // now list= {1, 2, 3, 4, 5,6,7}
list.WhereWithRemove(x=>x>3 && x<7).ToList(); // now list={1,2,7}
And code above will be optimized to the:
var tmp=list;
var listOfList=
tmp.WhereWithRemove(x=>x.Length==1) // now tmp contains: {x1,x2,x3,y1,y2,y3,y4,z1,z2}
.Select(x=>new KeyValuePair<string,List<string>>(x,
tmp.WhereWithRemove(y=>y.IndexOf(x)==0) //after every iteration tmp will contains {y1,y2,y3,y4,z1,z2}, {z1,z2}, {}
Instead of method WhereWithRemove can be added overload for the method Where with flag 'remove', for ex.:
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate,
bool remove=false);
WhereWithRemove will select and simultaneously remove the items from the original list? Not only that this would not work for all IEnumerable<T>s, it mutates the original list (If I remember correctly, every LINQ methods are pure functions, i.e. no side effects.).
There's the issue of enumerator invalidation as well, as enumerators will be invalid once the list has been mutated. (Though depending on the implementation this might not be a problem, such as allocating another storage for the removed items... but seems ugly in my opinion)
First time Linqer here but would't a simple grouping do what you want?
var result = from value in list
group value by value[0].ToString() into partition
select new KeyValuePair<string,List<string>>(
partition.Key,
(from item in partition where item!=partition.Key select item).ToList()
);
Yes grouping can be applied, but not if collection too big, because it becomes working slowly and can be optimized. Proposal to make a mechanism that will allow using the linq without additional overhead by changing current collection, while querying it.
In which case all the points raised by @Gnbrkm41 apply and make this sound like a very bad idea.
@ichensky While I agree with others that the proposed solution would be a bad idea, if GroupBy does not work for you, maybe another approach would? So: can you provide an example where GroupBy is too slow and your proposal works well?
Frankly, I'm suspicious about the claims here:
- The presented method is at least
O(n^2)just for access (compared to possiblyO(n)for the original), never mind the constant array shuffling. - The memory consumed is unlikely to be significantly better, given that the native output of
GroupByis largely identical in implication.
// Yields IGrouping<char, string>, which is functionally equivalent to the KeyValuePair<string, List<string>> you were using.
var grouped = listOfStuff.Where(x => x.Length > 1).GroupBy(x => x[0]).ToList();
- The method presented must be run single-threaded, whereas
GroupByis embarrassingly parallelizeable (ignoring ordering issues). - The method presented must be run over a type that has "deletable" storage. So, it can't be used with something like
File.ReadLines(...), or any of a host of similar transient enumerated collections.