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

spanWith, takeWhileWith #362

Closed
devvythelopper opened this issue Aug 11, 2021 · 4 comments
Closed

spanWith, takeWhileWith #362

devvythelopper opened this issue Aug 11, 2021 · 4 comments

Comments

@devvythelopper
Copy link

It would be nice to have a variant of span and a variant of takeWhile that do not just apply a predicate, but also fold over a value.

The practical application I have in mind is to span or takeWhile but only up to a maximum number of characters. This of course could be implemented if there was a spanMax and takeWhileMax but a generalized spanWith and takeWhileWith that also do a fold (in this case over the number of characters already selected by the predicate) seems to be the better choice to me.

@lehmacdj
Copy link

lehmacdj commented Sep 9, 2021

takeWhileMax n predicate = takeWhile predicate . take n and should have fairly similar performance characteristics to the dedicated version due to fusion. So at least for your practical application you should be good to go!

The more general version maybe still is necessary if you want to do something more complicated though; I don't see a way to cobble it together out of existing library functions in a good way.

@phadej
Copy link
Contributor

phadej commented Sep 9, 2021

@lehmacdj there is no implicit fusion (#348)

@lehmacdj
Copy link

Good to know, sad to hear though, but the discussion in that linked issue makes sense

@Lysxia
Copy link
Contributor

Lysxia commented May 16, 2022

This is implemented in PR #437 (which was originally motivated by issue #178, which is the same issue as this).

@Lysxia Lysxia closed this as completed May 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants