✅ Lazily combine sets
I'm not expecting this to be possible but just asking in case.
Is there any way to lazily combine sets, like if you did
xs.Concat(ys)
with regular enumerables? What I would be looking for is a way to take two IReadOnlySet<T>
s and combine them in a way that you get back another IReadOnlySet<T>
which is a combination of both of them, but which isn't evaluated immediately but rather lazily.10 Replies
Doesn't concat already do that?
That’s what I thought
IReadOnlySet<int> set1 = new HashSet<int> { 1, 2, 3 };
IReadOnlySet<int> set2 = new HashSet<int> { 3, 4, 5 };
IReadOnlySet<int> combinedSet = set1.Concat(set2).ToHashSet();
I don’t think combinedSet is evaluated until enumerated
Well, yes, but it doesn't retain the lookup capabilities
Yes it will, all
.To*
methods are evaluated immediatelySo you could evaluate it once you need it, or that isn't possible?
yeah idk
Just ditch the ToHashSet
Or you wrap it in a Lazy I guess, but that seems redundant
I don't even really know if there's any point to this, because performing a lookup in the set would require enumerating all the contents of the previous two sets anyway
The evaluation, I mean
actually no nvm
Could just do a custom
IReadOnlySet<T>
implementation which contains two sets internally where the look is essentially a.Contains(x) || b.Contains(x)