question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal for `takeuntil` method

See original GitHub issue

I 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:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nviecommented, Sep 29, 2022

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 think takeuntil(iterable, pred) should always be equivalent to takewhile(iterable, invert(pred)). So takewhile(numbers, isPositive) would be the same as takeuntil(numbers, isNegative).

Example:

takewhile([2, 4, 6, -1, -2, -3], isPositive)  // [2, 4, 6]
takeuntil([2, 4, 6, -1, -2, -3], isNegative)  // also [2, 4, 6], not including the -1

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!

0reactions
jbrialescommented, Dec 4, 2022

I still plan to make a pull request for this. Need to find some idle time in the coming weeks 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found