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

Add 'if' operator #96

Closed
dotdotcommadot opened this issue May 17, 2017 · 2 comments
Closed

Add 'if' operator #96

dotdotcommadot opened this issue May 17, 2017 · 2 comments

Comments

@dotdotcommadot
Copy link
Collaborator

Creating complex if / else structures is quite hard when your method chain is growing.
RxJS has a nice solution to it:
https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/if.md

It takes a 'condition' method, a 'thenSource' and an 'elseSource' as parameters.

@brianegan
Copy link
Collaborator

brianegan commented May 18, 2017

Ah, interesting one. Not totally opposed to implementing it, but would love a bit more info. I'd rather not add an abstraction if a simple language construct would do. Could you provide an example where this is more useful than a normal function?

Just as a comparison for discussion, here's what I'm thinking:

Normal function

Stream<int> ifStream(bool shouldRun) => shouldRun
    ? new Observable<int>.just(42)
    : new Observable<int>.empty();

Stream<int> elseStream(bool shouldRun) => shouldRun
    ? new Observable<int>.just(42)
    : new Observable<int>.just(56);

// Normal usage
ifStream(true).listen(print); // prints 42
ifStream(false).listen(print); // prints nothing

// Nice with flatmap
new Observable<bool>.just(false)
    .flatMap(elseStream)
    .listen(print); // prints 56

If factory

var shouldRun = true;

Stream<int> ifStream = new Observable.if(() => shouldRun,
    new Observable.just(42));

Stream<int> elseStream = new Observable.if(() => shouldRun,
    new Observable.just(42), 
    new Observable.just(56));

@brianegan
Copy link
Collaborator

brianegan commented Jun 6, 2017

Since i haven't heard back for a while, gonna close this out for now. Please write back and re-open if you'd like us to reconsider!

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