1

I want to combine multiple IncrementalValuesProvider<T>

I don't find var result = a.Combine(b.Combine(c)); very appealing, because result.Left.Left and result.Right.Left etc are not intuitive at all.

Is there some way I can combine multiple IncrementalValuesProvider<T> into a single custom object? For example

  IncrementalValuesProvider<One> one = ...;
  IncrementalValuesProvider<Two> two = ...;
  IncrementalValuesProvider<Three> three = ...;

  IncrementalValuesProvider<Combined> = one.Combine(two.Collect())
    .Select(x => new { One = x.Left, Two = x.Right })
    .Combine(three.Collect())
    .Select(x => new Combined {
      One = x.Left.One,
      Two = x.Left.Two,
      Three = x.Right
    });

Absolutely anything will do as long as I don't have to keep traversing left/left/right/left :)

Peter Morris
  • 20,174
  • 9
  • 81
  • 146
  • Note: you need to be very careful when combining providers, as it ties the incrementality of each one together. That is, as soon as one of the providers changes, any code which depends on the combined ones will have to re-run. – Chris Sienkiewicz Aug 02 '22 at 21:26
  • Thanks Chris. That's exactly what I need to happen :) – Peter Morris Aug 04 '22 at 07:24

1 Answers1

1

The trick is to convert IncrementalValuesProvider (plural) to IncrementalValueProvider (single).

IncrementalValueProvider<ImmutableArray<one>> newOne = one.Collect();

Then you can do this

var oneAndTwo = one.Combine(two)
  .Select((x, cancellationToken) => new 
  {
    One = x.Left,
    Two = x.Right

IncrementalValueProvider<OneAndTwoAndThree> oneAndTwoAndThree = oneAndTwo.Combine(three)
  .Select((x, cancellationToken) => new OneAndTwoAndThree(
    x.Left.One,
    x.Left.Two,
    x.Right);
Peter Morris
  • 20,174
  • 9
  • 81
  • 146