cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

Deprecate when.iterate and when.unfold #370

Closed briancavalier closed 10 years ago

briancavalier commented 10 years ago

In favor of most.js streams, which have much nicer versions of both, complete with filtering, transforming, reducing, etc.

astorije commented 9 years ago

Hi @briancavalier,

It seems to me that the version in most.js allows for creating infinite structures while when.iterate was nice to create an infinite sequence of actions. Although I might be wrong in the design, in this previous sentence, I couldn't find an example on how to translate the following code using when.iterate into code using most.iterate or most.unfold:

require('when').iterate(function(x) {
    return x+1;
}, function(x) {
    return x >= 10;
}, function(x) {
    console.log(x);
}, 0).done();

Could you help? In particular, how to rig the end condition and the handler in when.iterate (if possible)?

briancavalier commented 9 years ago

most.js allows for creating infinite structures while when.iterate was nice to create an infinite sequence of actions

That's quite an interesting distinction, @astorije. I'll give that some thought. I've been considering other options for keeping when.unfold/iterate. For example, we could move them to a contrib package, like when-unfold or something, since they are less commonly used.

I couldn't find an example on how to translate the following code

I'll take a shot at it :) Using ES6 for brevity, here's with most.iterate

most.iterate(x => x+1, 0)
    .takeWhile(x => x < 10)
    .observe(x => console.log(x))
    .done();

And with most.unfold:

most.unfold(x => { value: x, seed: x+1, done: x >= 10 }, 0)
    .observe(x => console.log(x))
    .done();

Does that help? Let me know if you any questions!

astorije commented 9 years ago

It does help, thanks a lot @briancavalier!

I now have a better understanding of what I want to do and how to plug (when|most).iterate to it. However, I do not need a full-featured lib for that, and will end up using a custom version of when.iterate.

That might confirm that when.iterate is better off its own package, but that's your call. All in all, I'm happy I could find the code of when.iterate and when.unfold because they really correspond to what I want to do, without relying on 4 different methods, but that might only be good for my current use case...

mnahkies commented 8 years ago

@briancavalier The API documentation does not seem to reflect this deprecation, is this intentional?

Additionally, the documentation does not state that the predicate may return a promise for a value whilst the code and tests indicate that it can.

https://github.com/cujojs/when/blob/master/docs/api.md#whenunfold