1

I have an hot observable of a sequence of items that are have a key that identifies a specific sub-stream. I'm interested to map those M streams into N with N < M (group them into N buckets). For each bucket, each time an element arrives, I want to apply a function to the latest element of each underlining sequence of that group. I've prior knowledge of both N and M groups.

In the following sample, we have a sequence of quote for four fruits. I want to map those streams into two, by the type of fruit (Apple or Pear). For each group I want to collect the last known quote of each fruit.

class Input {
    public string ProductID {get;set;}
    public string ProductType {get;set;}
    public int    Price {get;set;}
}

class Output {
    public string  ProductType {get;set;}
    public Input[] Underlining {get;set;}
}

var obs = new List<Input> {
    new Input { ProductID = "Stark",    ProductType = "Apple", Price = 21 },
    new Input { ProductID = "Jonagold", ProductType = "Apple", Price = 12 },
    new Input { ProductID = "Williams", ProductType = "Pear",  Price = 33 },
    new Input { ProductID = "Beth",     ProductType = "Pear",  Price = 22 },
    new Input { ProductID = "Stark",    ProductType = "Apple", Price = 43 },
    new Input { ProductID = "Williams", ProductType = "Pear",  Price = 55 },
    new Input { ProductID = "Beth",     ProductType = "Pear",  Price = 66 },
    new Input { ProductID = "Jonagold", ProductType = "Apple", Price = 77 },
    new Input { ProductID = "Jonagold", ProductType = "Apple", Price = 25 },
    new Input { ProductID = "Williams", ProductType = "Pear",  Price = 77 },
    new Input { ProductID = "Beth",     ProductType = "Pear",  Price = 13 },
    new Input { ProductID = "Stark",    ProductType = "Apple", Price = 21 },
}.ToObservable();

IObservable<Output> result = obs.GroupBy ... Select ... Concat ... ; // I'm a bit loss here 


result.Dump();

Expected result:

{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 21 }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 21 },     { ProductID = "Jonagold", Price = 12 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 23 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 23 },     { ProductID = "Beth",     Price = 22 }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = **43** }, { ProductID = "Jonagold", Price = 12 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = **55** }, { ProductID = "Beth",     Price = 22 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 55 },     { ProductID = "Beth",     Price = **66** }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 43 },     { ProductID = "Jonagold", Price = **77** }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 43 },     { ProductID = "Jonagold", Price = **25** }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = **77** }, { ProductID = "Beth",     Price = 66 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 77 },     { ProductID = "Beth",     Price = **13** }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = **21** }, { ProductID = "Jonagold", Price = 25 }] }
2
  • I'm struggling to understand your question. I appreciate the example code, but you haven't explained fully what the data means. The first paragraph does mention SourceKey, but nothing about GroupKey, and Value. Can you please change these properties to something like Name, Location, & Age` to try and make it easier to understand? Then please explain how the expect result would come out. Also, compilable code for obs and your result would be great. Jan 24, 2016 at 10:14
  • Thanks reading and taking time to reply. I've tried to do as you suggested. I've omitted in the expected result the ProductType to improve readability. Jan 24, 2016 at 12:24

1 Answer 1

2

I think this is what you want:

var outputs =
    obs
        .GroupBy(x => x.ProductType)
        .Select(xs =>
            xs
                .Scan(
                    new Dictionary<string, Input>(),
                    (d, x) => { d[x.ProductID] = x; return d; })
                .Select(x => new Output()
                {
                    ProductType = xs.Key,
                    Underlining = x.Values.ToArray(),
                }))
        .Merge();

I used outputs.Select(x => $"{{ ProductType = \"{x.ProductType}\", Underlining = [{String.Join(", ", x.Underlining.Select(y => $"{{ ProductID = \"{y.ProductID}\", Price = {y.Price} }}"))}] }}") to get the following output to test it:

{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 21 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 21 }, { ProductID = "Jonagold", Price = 12 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 33 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 33 }, { ProductID = "Beth", Price = 22 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 43 }, { ProductID = "Jonagold", Price = 12 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 55 }, { ProductID = "Beth", Price = 22 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 55 }, { ProductID = "Beth", Price = 66 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 43 }, { ProductID = "Jonagold", Price = 77 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 43 }, { ProductID = "Jonagold", Price = 25 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 77 }, { ProductID = "Beth", Price = 66 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 77 }, { ProductID = "Beth", Price = 13 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 21 }, { ProductID = "Jonagold", Price = 25 }] } 
2
  • Nice, I forgot about Scan and was struggling with this.. but where does the selection of Max value happen?
    – supertopi
    Jan 24, 2016 at 15:06
  • @supertopi - What "Max"? The requirement was "last known quote of each fruit". Did I miss something? Jan 24, 2016 at 22:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.