Skip to content

[Feature request] buffer with predicate which determines when buffer must be flushed #369

Open
@anatoly-spb

Description

@anatoly-spb

Hello!

I have simple example with character stream:

  auto source = rxcpp::observable<>::iterate(
    std::initializer_list<char>{ '1', '2', '3', '$', '5', '$', '7', '8', '$' }
  );

I want to get std::vector for each sub sequence terminated by '$'. I know that there is buffer operator ( http://reactivex.io/documentation/operators/buffer.html ) with count and time overloads. But what about buffer with predicate which determines when buffer must be flushed?

Now I have the following workaround:

  auto source = rxcpp::observable<>::iterate(
    std::initializer_list<char>{ '1', '2', '3', '$', '5', '$', '7', '8', '$' }
  );

  auto vector_stream = source
    | rxcpp::operators::flat_map([](char ch) {
    return rxcpp::observable<>::create<std::vector<char>>(
      [ch](rxcpp::subscriber<std::vector<char>> s) {
      static std::vector<char> a;
      if (ch == '$') {
        s.on_next(a);
        a.clear();
      }
      else {
        a.push_back(ch);
      }
    });
  });

  vector_stream
    .subscribe([](const std::vector<char> &v) {
    std::cout << "consume vector: ";
    std::copy(v.begin(), v.end(), std::ostream_iterator<char>(std::cout, " "));
    std::cout << std::endl;
  });

I am newcomer, so may be you point me more correct and elegant RX way to solve my task.

Thanks in advance,
Anatoly Shirokov

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions