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

Operator: takeUntil #7

Closed
OliverJAsh opened this issue Feb 15, 2017 · 7 comments
Closed

Operator: takeUntil #7

OliverJAsh opened this issue Feb 15, 2017 · 7 comments

Comments

@OliverJAsh
Copy link

Hi!

Are there plans to add a takeUntil operator to this library?

If so, I was wondering if we would have inclusive (take until and including) and exclusive versions (take until and excluding the first falsy predicate).

@OliverJAsh
Copy link
Author

An example of where this would be useful:

[1,2,3,4].takeUntil(x => x >= 3)
// [ 1, 2 ]
[1,2,3,4].takeUntilInclusive(x => x >= 3)
// [ 1, 2, 3 ]

I'm not sure if takeUntilInclusive is a bad idea or not, or it the same can be achieved with other operators?

@OliverJAsh
Copy link
Author

Upon investigation, it looks like takeWhileInclusive might be a better idea here: http://stackoverflow.com/a/22472610/5932012

@mattpodwysocki
Copy link
Contributor

@OliverJAsh added takeUntil as per spec for AsyncIterableX

@OliverJAsh
Copy link
Author

Hi @mattpodwysocki. The takeUntil included accepts a promise parameter, however I would like a version that accepts a function parameter, like what you see in RxJava http://reactivex.io/RxJava/javadoc/rx/Observable.html#takeUntil(rx.functions.Func1)

Would you consider adding this?

@OliverJAsh
Copy link
Author

To anyone who comes here needing this. Here's a custom implementation you can use:

export const takeUntil = <T>(fn: (value: T) => boolean) => (
    asyncIterable: AsyncIterable<T>,
): AsyncIterable<T> =>
    Ix.AsyncIterable
        .from(asyncIterable)
        .map(value => {
            const shouldEnd = fn(value);
            return shouldEnd
                ? [{ value, done: false }, { value: undefined, done: true }]
                : [{ value, done: false }];
        })
        .flatMap(iterable => Ix.AsyncIterable.from(iterable))
        // We know `value` has type `T` because of the `takeWhile` guard
        // https://github.com/ReactiveX/IxJS/issues/44
        .takeWhile(x => !x.done)
        .map(({ value }) => value as T);

@OliverJAsh
Copy link
Author

OliverJAsh commented Aug 11, 2018

Similar feature request for RxJS, which seems to have a good amount of support: ReactiveX/rxjs#2420.

If I sent a PR for this, would it be reviewed and potentially accepted?

Related: https://github.com/martinsik/rxjs-plus#takewhileinclusive

@trxcllnt
Copy link
Member

@OliverJAsh yes, definitely! @mattpodwysocki might have more time than me, I'm pretty slammed till the end of the month, but we should be able to work it in.

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

3 participants