Giter Site home page Giter Site logo

Comments (6)

victimsnino avatar victimsnino commented on May 24, 2024

Hi!
Thanks for feature request =)

Not sure, if i get your idea right.... to emit values via behavior_subject you need to "push" this value somehow. For example

rpp::subjects::behavior_subject<std::map<std::string, std::pair<double, double>>> subj;
subj.get_observer.on_next(?????);

So, target expected value anyway is constructed during on_next stage.

What you are requesting is kind of scan but with caching, right?

rpp::subjects::publish_subject<double> some_inner_subject;

auto observable = some_inner_subject.get_observable()
| rpp::operators::scan(std::map<std::string, std::pair<double, double>>{}, 
                       [](auto&& map, double value) {
                           // some complex rule
                           map[std::to_string(value)] = std::pair{value, value};
                           return map;
                       });

some_inner_subject.get_observer().on_next(1);
// Expected to obtain {{"1":{1,1}} ??????
observable.subscribe([](auto v) { .....});

some_inner_subject.get_observer().on_next(2);
// Expected to obtain {"1":{1,1}, "2":{2,2}} ??????
observable.subscribe([](auto v) { .....});

Anyway, do you have any ideas how it can be manipulated from user's perspective?

from reactiveplusplus.

mincequi avatar mincequi commented on May 24, 2024

Hi! Thanks for feature request =)

Not sure, if i get your idea right.... to emit values via behavior_subject you need to "push" this value somehow. For example

True, and each time I push a new value, I have to construct the entire object, which might be expensive. Image this previously mentioned map, which might contain many many elements. So, each time I want to emit a new map where only one entry changed, I have to copy the whole map for changing only a single entry.

Anyway, do you have any ideas how it can be manipulated from user's perspective?

Hmm, good question.
When I check the source of v1's behavior_subject, there is a getter for the value: get_value. One could return a reference to the internal state value.
Then we need something like on_next_with_latest_state() or similar....

Not sure, if this can help (or inspire), but i am also using Kotlin Flow and I definitely prefer this over RxJava (and the strict syntax of Rx).

from reactiveplusplus.

victimsnino avatar victimsnino commented on May 24, 2024

Hmm, i see your issue. Looks like it should be custom operator or something like this... i don't think it is good idea to provide ability to modify subject's state directly - it is unsafe and doesn't follow "functional" way.

Isn't it enough to make something like...

auto new_map = subject.get_value();
new_map[some_key].some_value = some_new_value;
subject.on_next(std::move(new_map));

It can be easily wrapped to custom operator if you prefer it. Or such an solution is missing something?

from reactiveplusplus.

mincequi avatar mincequi commented on May 24, 2024

Understood your concerns. My current solution is exactly what you described and this is perfectly reasonable for small maps (and probably even bigger ones).
Afaik, java (and other languages) do not do deep copies and calling on_next on an object would not result in a deep copy. However, it seems to be that way in C++. Alternatively, I could create a pointer and use that as the subject's type....
Not sure, about the custom operator, though.

from reactiveplusplus.

victimsnino avatar victimsnino commented on May 24, 2024

Afaik, java (and other languages) do not do deep copies and calling on_next on an object would not result in a deep copy.

In my understanding, all variables in garbage collector based languages are kind of shared_ptrs....

However, it seems to be that way in C++.

To be honest, current example would make 1 copy from get_value() and 1 move inside on_next. Not to much =)

Alternatively, I could create a pointer and use that as the subject's type....

Yeah, shared_ptr also the option in this case. Not such an elegant, but no any copies would happen at all... =)

from reactiveplusplus.

victimsnino avatar victimsnino commented on May 24, 2024

Looks like no any actions required there

from reactiveplusplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.