Proposal for `takeuntil` method
See original GitHub issueI was looking for a “takeuntil” method across existing utils,
and the closest I found was takewhile
.
While takewhile
“produces elements from the iterable as long as the predicate is true”,
I’d expect takeuntil
to “produce elements from the iterable as long as the predicate is true”.
E.g.
test('takeWhile', () => {
expect([...takewhile([1, 2, -1, 3], x => x > 0)]).toStrictEqual([1, 2]);
});
test('takeuntil', () => {
expect([...takeuntil([1, 2, -1, 3], x => x < 0)]).toStrictEqual([1, 2, -1]);
});
Would it make sense to add this function to the library?
I.e. having
function* takeuntil(iterable, predicate) {
for (const value of iterable) {
yield value;
if (predicate(value)) return;
}
}
compared to
export function* takewhile<T>(iterable: Iterable<T>, predicate: Predicate<T>): Iterable<T> {
for (const value of iterable) {
if (!predicate(value)) return;
yield value;
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How To Use the takeUntil RxJS Operator to Manage ...
The solution is to compose the subscriptions with the takeUntil operator and use a subject that emits a truthy value in the ngOnDestroy ......
Read more >Do I need to `complete()` takeUntil Subject inside ngOnDestroy?
Short answer, no this is not needed, but it also doesn't hurt. Long answer: Unsubscribing / completing in angular is only needed when...
Read more >2.x Proposal: disposeOn · Issue #4646 · ReactiveX/RxJava
It would look very similar to takeUntil() , but with disposable semantics (not calling ... Compose it with never() static Observable method.
Read more >takeUntil - Learn RxJS
If you only need a specific number of values, try take! . Ultimate RxJS. . Examples. Example 1: Take values until timer emits....
Read more >Angular: Managing RxJS Observable Subscriptions with ...
Approach Two: RxJS takeUntil(). The takeUntil operator helps us get a bit further in making a clean break with our Subscription.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can see the usefulness of a function like that, because using just
takewhile
, you always read past the item that breaks the predicate, and you lose that information. It may be useful to have a version that retains and returns the otherwise discarded value.To me,
takeuntil
is not the best name for such function, though. I thinktakeuntil(iterable, pred)
should always be equivalent totakewhile(iterable, invert(pred))
. Sotakewhile(numbers, isPositive)
would be the same astakeuntil(numbers, isNegative)
.Example:
The proposed implementation here is more like a
takewhileInclusive
. That said, I think that that is a useful function to add, so I’d be happy to merge it if you want to contribute that!Thanks!
I still plan to make a pull request for this. Need to find some idle time in the coming weeks 😃