如何将IObservable投影到Rx中按键分组的元素列表中?

时间:2016-03-13 02:02:55

标签: .net system.reactive reactive-programming

鉴于IObservable(Of T)我们如何将其转换为IObservable(Of List(Of T)),其中列出了哪些元素按某个键分组? 使用GroupBySelectScan运算符我已设法将源分区为可观察的,以生成每个键的所有元素的列表。我不知道如何将这些列表进一步连接成一个列表。

    Dim source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.ToObservable()

    Dim keySelector = Function(element As Integer) As Integer
                          Return element Mod 3
                      End Function

    Dim result = source.GroupBy(Of Integer)(keySelector) _
                 .Select(Function(gr)
                             Return gr.Scan(New List(Of Integer), _
                                            Function(integers, current)
                                                integers.Add(current)
                                                Return integers
                                            End Function)
                         End Function)

    result.Subscribe(Sub(gr) gr.Subscribe(Sub(lst)
                                              Console.WriteLine(String.Join(",", lst))
                                          End Sub))

它产生以下输出:

1
2
3
1,4
2,5
3,6
1,4,7
2,5,8
3,6,9
1,4,7,10

虽然我需要它:

1
1,2
1,2,3
1,4,2,3
1,4,2,5,3
1,4,2,5,3,6
1,4,7,2,5,3,6
1,4,7,2,5,8,3,6
1,4,7,2,5,8,3,6,9
1,4,7,10,2,5,8,3,6,9

2 个答案:

答案 0 :(得分:2)

这可以满足您的需求:

Dim result = _
    Observable _
        .Create(Of List(Of Integer))( _
            Function (o)
                Dim keysFound = 0
                Dim keyOrder = New Dictionary(Of Integer, Integer)
                Return _
                    source _
                        .Do( _
                            Sub (x)
                                Dim k = keySelector(x)
                                If Not keyOrder.ContainsKey(k) Then
                                    keyOrder.Add(k, keysFound)
                                    keysFound = keysFound  + 1
                                End If
                            End Sub) _                      
                        .Scan( _
                            New List(Of Integer), _
                            Function(integers, current)
                                integers.Add(current)
                                Return integers
                            End Function) _
                        .Select(Function(integers) _
                            integers.OrderBy(Function (x) _
                                keyOrder(keySelector(x))).ToList()) _
                        .Subscribe(o)
            End Function)

result.Subscribe(Sub(gr) Console.WriteLine(String.Join(",", gr)))

我得到了这个结果:

1
1,2
1,2,3
1,4,2,3
1,4,2,5,3
1,4,2,5,3,6
1,4,7,2,5,3,6
1,4,7,2,5,8,3,6
1,4,7,2,5,8,3,6,9
1,4,7,10,2,5,8,3,6,9

答案 1 :(得分:0)

这不是我的问题的确切答案,因为它不会产生请求的输出类型,但考虑到性能,我决定使用@Carsten建议测试一种使用Dictionary的新方法。我的源生成的值非常快,我可能不需要处理值,因此我可以执行.Throttle.Sample。鉴于此,我将每个键的所有值收集到列表中,并仅发出Dictionary(Of Integer,Of IList(Of T))。在订阅者部分,在限制或采样之后,接收的字典被展平。

    Dim source = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.ToObservable()

    Dim keySelector = Function(element As Integer) As Integer
                          Return element Mod 3
                      End Function

    Dim result = source.Select(Function(i)
                                   Return New With {.reminder = keySelector(i), .value = i}
                               End Function) _
                        .Scan(ImmutableDictionary(Of Integer, IImmutableList(Of Integer)).Empty, _
                              Function(accumulate, current)
                                  Dim builder = accumulate.ToBuilder()
                                  If Not builder.ContainsKey(current.reminder) Then
                                      builder.Add(current.reminder, ImmutableList(Of Integer).Empty)
                                  End If
                                  Dim currentList = builder(current.reminder)
                                  builder(current.reminder) = currentList.Add(current.value)
                                  Return builder.ToImmutable()
                              End Function)


    result.Throttle(TimeSpan.FromMilliseconds(100)) _
        .Subscribe(Sub(dictionary)
                       Console.WriteLine( _
                           String.Join(",", dictionary.SelectMany(Function(pair)
                                                                      Return pair.Value
                                                                  End Function)))
                   End Sub)

它产生了这个:

3,6,9,1,4,7,10,2,5,8

有趣的是,ImmutableDictionary(Of Tkey, TValue)在添加密钥时会对其进行排序,而Dictionary(Of TKey, TValue)则不会。现在这不是一个真正的问题。