Add 'Select' ext fns for Update/TableUpdate

This commit is contained in:
John Colvin committed 2022-04-19 11:42:39 -04:00
1 parent 2d112a86af
commit 8b5d79847f
1 file changed
+21
+21
View File
@@ -498,11 +498,32 @@ namespace bts.api.core {
});
}
public static Update<TResult, TKey> Select<T, TKey, TResult>(
this Update<T, TKey> update,
Func<T, TResult> result_selector,
Func<TResult, TKey> key_selector) where TKey : IEquatable<TKey> =>
new(
update.all.Select(result_selector),
update.added.Select(result_selector),
update.changes.Select(ch => new Change<TResult>(
result_selector(ch.Old), result_selector(ch.New))),
update.removed.Select(result_selector),
key_selector);
public static IObservable<TableUpdate<T,TKey>> Table<T,TKey>(this IObservable<Update<T,TKey>> source) where TKey : IEquatable<TKey> =>
source.Scan(
new TableUpdate<T,TKey>(ImmutableDictionary<TKey,T>.Empty, new T[0], new Change<T>[0], new T[0], null),
(table, update) => TableUpdate<T,TKey>.update(table,update));
public static IObservable<TableUpdate<TResult, TKey>> Table<T, TKey, TResult>(
this IObservable<Update<T, TKey>> source,
Func<T, TResult> selector,
Func<TResult, TKey> key_selector) where TKey : IEquatable<TKey> =>
source.Scan(
TableUpdate<TResult, TKey>.empty,
(table, update) => TableUpdate<TResult, TKey>.update(
table, update.Select(selector, key_selector)));
public static IObservable<TableUpdate<T, TKey>> Table<T, TKey>(this IObservable<IReadOnlyDictionary<TKey, T>> source, Func<T, TKey> key) where TKey : IEquatable<TKey> =>
source.Table(key, EqualityComparer<T>.Default);