-
Notifications
You must be signed in to change notification settings - Fork 607
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 limit combinator to Stream #3113
Add limit combinator to Stream #3113
Conversation
Emits the first n elements, raising an error if there are more elements
@sgjbryan Good evening, Sam. Thanks for the PR. As it happens, something like this is also included in the The name Finally, rather than issuing an |
Great, thanks for the quick review! It seems to me that the imperative tone of I would normally agree with raising a more specific exception, but it didn't appear to be the convention here (and I couldn't find any natural place to define a narrower exception). In principle I'd be happy to introduce something like |
Just done some digging and I'm guessing this is the equivalent |
I don't think we should add a custom exception type for this. We don't do it for other combinators with similar error cases (e.g. |
Well, The other reason for that is that this new method is a bit like an assertion, like Scala |
That's true, I was probably a bit sloppy calling it imperative - I think it's probably the fluency (for want of a better word) that seems a bit out of place to me. That being said, there are examples of both (e.g. combinators like To me, I don't have a strong opinion though, so if general opinion is with |
|
98141ab
to
212a914
Compare
What
Adds a
limit
combinator toStream
, which emits the first n elements and raises an error if there are more elements.Why
I've implemented this a few times recently, and it seems to be sufficiently general to be worth implementing efficiently as a canoncial combinator. Example use case might be a middleware which enforces size limits on streams.
I'm not sure what the desired approach is to combinator generality - so it's completely fine if the consensus here is that it's not useful enough to warrant being included in the
Stream
API. Also hppy to rework the naming if desired, alternatives might belimitOrError
or something likemaxSize
.Thanks for opening a PR!
Target the
main
branch if this PR is targeted for the 3.x series and theseries/2.5.x
branch if targeted for the 2.5.x series.If the CI build fails due to Scalafmt, run
sbt scalafmtAll
and push the changes (generally a good idea to run this prior to opening the PR).Feel free to ask questions on the fs2 and fs2-dev channels on the Typelevel Discord server.