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

Add .interleaveOrdered to combine sorted streams (i.e. "merge-sort") #3110

Merged
merged 6 commits into from
Jan 20, 2023

Conversation

ivan-klass
Copy link
Contributor

An implementation of "merge-sort" merging for two streams of sorted data.
This might be useful in practice when combining streams from different underlying sources.

Discord conversation

@mpilquist
Copy link
Member

Thanks! Haven't looked at this PR yet but it reminded me of:

@mpilquist
Copy link
Member

Approach looks good to me (though haven't done a line by line review yet). I lean towards coming up with a different name, as merge in fs2 implies concurrency whereas this combinator doesn't do concurrent pulls of the two sources (and I agree it shouldn't). Our deterministic combinators that combine two streams include zip and variants, interleave. Any ideas?

@ivan-klass
Copy link
Contributor Author

Approach looks good to me (though haven't done a line by line review yet). I lean towards coming up with a different name, as merge in fs2 implies concurrency whereas this combinator doesn't do concurrent pulls of the two sources (and I agree it shouldn't). Our deterministic combinators that combine two streams include zip and variants, interleave. Any ideas?

Totally agree. While "merge" is associated with concurrency, "zip" usually means a product over original element type, so I think "interleave" is a good verb, what about "interleaveSorted"?
Also, could you please suggest a better suite file to place a test? Or should it be a new one? Thanks

@mpilquist
Copy link
Member

mpilquist commented Jan 18, 2023

+1 for interleaveSorted or interleaveOrdered

Copy link
Member

@mpilquist mpilquist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me besides the name bike-shedding. Thanks!

@ivan-klass ivan-klass changed the title Add .mergeSorted to merge sorted streams into one (i.e. "merge-sort") Add .interleaveOrdered to combine sorted streams into one (i.e. "merge-sort") Jan 18, 2023
@ivan-klass ivan-klass changed the title Add .interleaveOrdered to combine sorted streams into one (i.e. "merge-sort") Add .interleaveOrdered to combine sorted streams (i.e. "merge-sort") Jan 18, 2023
@ivan-klass
Copy link
Contributor Author

ivan-klass commented Jan 18, 2023

@mpilquist @diesalbla I've updated method name and found a better place for test

UPD: Also refactored the implementation to much more readable with ".uncons"

core/shared/src/main/scala/fs2/Stream.scala Outdated Show resolved Hide resolved
core/shared/src/main/scala/fs2/Stream.scala Outdated Show resolved Hide resolved
@mpilquist
Copy link
Member

This combinator must be implemented with stepLeg and not uncons. See #1678 (comment) for details as to why.

@ivan-klass
Copy link
Contributor Author

This combinator must be implemented with stepLeg and not uncons. See #1678 (comment) for details as to why.

@mpilquist thank you for the explanation, just reverted the last commit

@diesalbla diesalbla merged commit 597102b into typelevel:main Jan 20, 2023
@ivan-klass ivan-klass deleted the merge-sorted branch January 20, 2023 17:18
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

Successfully merging this pull request may close these issues.

3 participants