C
C#2y ago
Thinker

✅ 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
Denis
Denis2y ago
Doesn't concat already do that?
Connor
Connor2y ago
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
Thinker
Thinker2y ago
Well, yes, but it doesn't retain the lookup capabilities Yes it will, all .To* methods are evaluated immediately
Denis
Denis2y ago
So you could evaluate it once you need it, or that isn't possible?
Thinker
Thinker2y ago
yeah idk
Connor
Connor2y ago
Just ditch the ToHashSet
Denis
Denis2y ago
Or you wrap it in a Lazy I guess, but that seems redundant
Thinker
Thinker2y ago
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
Denis
Denis2y ago
The evaluation, I mean
Thinker
Thinker2y ago
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)
Want results from more Discord servers?
Add your server