Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent Rx example #4

Closed
xgrommx opened this issue Aug 15, 2015 · 9 comments
Closed

Equivalent Rx example #4

xgrommx opened this issue Aug 15, 2015 · 9 comments

Comments

@xgrommx
Copy link

xgrommx commented Aug 15, 2015

Hi @dyoder. How can I convert this example to your library?

const {fromEvent} = Rx.Observable;

const inc = fromEvent(incEl, 'click').map(_ => 1);
const dec = fromEvent(decEl, 'click').map(_ => -1);

dec.merge(inc).scan((a, b) => a + b, 0).subscribe(::console.log);
@dyoder
Copy link
Member

dyoder commented Aug 16, 2015

Thanks for your question!

We have something roughly equivalent in the web app counter example.

The closest direct port would look like:

const {start, flow, events, map, observe} = FairmontReactive;

var counter = { value: 0 };

start(flow([
  events('click', incEl), 
  map(_ => counter.value++)
]));

start(flow([
  events('click', decEl),
  map(_ => counter.value--)
]));

start(flow([
  events('change', observe(counter)),
  map(::console.log)
]));

There's no equivalent at present of merge or scan. Adding them seems like a reasonable thing to do. If we did that, we could then do something more directly comparable.

const {start, flow, events, map, observe, merge, scan} = FairmontReactive;

var inc = flow([
  events('click', incEl), 
  map(_ => 1)
]);

var dec = flow([
  events('click', decEl),
  map(_ => -1)
]);

start(flow([
  merge(inc, dec),
  scan((a, b) => a + b, 0),
  map(::console.log)
]));

This was referenced Aug 16, 2015
@dyoder
Copy link
Member

dyoder commented Aug 16, 2015

We're also considering a function go that would get rid of the constant start(flow([ bits and allowing flow to take argument lists as well as arrays. In that case, the last version would become:

const {start, flow, events, map, observe, merge, scan} = FairmontReactive;

var inc = flow(events('click', incEl), map(_ => 1)));

var dec = flow(events('click', decEl), map(_ => -1)));

go(
  merge(inc, dec),
  scan((a, b) => a + b, 0),
  map(::console.log)
);

@dyoder
Copy link
Member

dyoder commented Aug 16, 2015

Made a bunch of minor corrections to the code above.

@dyoder
Copy link
Member

dyoder commented Aug 17, 2015

merge is now coded in master as combine (because there's already a merge in fairmont-helpers that does an unrelated thing)

@dyoder
Copy link
Member

dyoder commented Aug 17, 2015

Also coded up accumulate.

@xgrommx
Copy link
Author

xgrommx commented Aug 17, 2015

@dyoder What is accumulate?

@dyoder
Copy link
Member

dyoder commented Aug 17, 2015

@xgrommx accumulate is scan.

This is implemented and available in Beta 29. There is a bug: see #8.

So the equivalent of your original example is now:

var inc = flow(events('click', incEl), map(_ => 1)));

var dec = flow(events('click', decEl), map(_ => -1)));

go(
  combine(inc, dec),
  accumulate((a, b) => a + b, 0),
  map(::console.log)
);

Thanks again for your question and for motivating these new additions.

@dyoder dyoder closed this as completed Aug 17, 2015
@xgrommx
Copy link
Author

xgrommx commented Aug 18, 2015

@dyoder Nice! Also your library was added to my book http://xgrommx.github.io/rx-book/content/similar_libraries/index.html =)

@dyoder
Copy link
Member

dyoder commented Aug 18, 2015

@xgrommx that is amazing list. by far the most comprehensive i've seen. thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants