Replies: 4 comments 7 replies
-
Not built-in, but yeah, each of those could deeeeeeeefinitely be done better. Each one of those appears to perform a full clone of the existing collection upon each received ChangeSet. That wouldn't be too terrible if the collections were For these specific scenarios, you could optimize pretty heavily. public static IObservable<int> Count(this IObservable<IChangeSet<T>> source)
=> source.Scan(
source: source,
seed: 0,
accumulator: static (count, changeSet) =>
{
var nextCount = count;
foreach(var change in changeSet)
{
if (change.Reason == ChangeReason.Add)
++nextCount;
else if (change.Reason == ChangeReason.Remove)
--nextCount;
}
return nextCount;
}); public static IObservable<int> Count(this IObservable<IChangeSet<T>> source)
=> source.Count().Select(count => count != 0); The last one, |
Beta Was this translation helpful? Give feedback.
-
An optimized version of @JakenVeina Before I released Dynamic Data, I experimented with every conceivable data structure and one of the worst performing (memory and speed considered) ones was the immutable collections. For all round performance, the standard list proved to be much superior to all else, mainly because it can be pre-sized and that operations when the index if known are super fast, |
Beta Was this translation helpful? Give feedback.
-
I would do like this instead: IObservable<bool> hasAny = changeSet.ToObservableCache().CountChanged.Select(n => n > 0);
This is even more simple, but the same idea: IObservable<int> collectionSize = changeSet.ToObservableCache().CountChanged;
Same as the first, but filter the changeset BEFORE it comes in.
IObservable<bool> hasAny = changeSet.Filter(condition).ToObservableCache().CountChanged.Select(n => n > 0); This approach still uses an intermediate collection, like |
Beta Was this translation helpful? Give feedback.
-
OK, I've found this scenario quite common:
The problem is that it doesn't push 0 when the filtered colleciton is empty. How to proceed? |
Beta Was this translation helpful? Give feedback.
-
I find myself writing methods like this too often:
Is there any better (more idiomatically) do things like this?
Beta Was this translation helpful? Give feedback.
All reactions