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

Incrementing Subsequences #88

Open
nipafx opened this issue Feb 7, 2025 · 4 comments
Open

Incrementing Subsequences #88

nipafx opened this issue Feb 7, 2025 · 4 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@nipafx
Copy link

nipafx commented Feb 7, 2025

I found one more operation in my notes, but I've only implemented that for fun and haven't used it in practice, so not sure how much sense it makes: It's about creating groups of variable length, where within each group the elements are in increasing order according to their natural order or a given comparator. E.g.:

Stream.of(1, 3, 5, 6, 2, 4)
	.gather(incrementing()) // Stream<List<Integer>>
	.forEach(System.out::println); // [1, 3, 5] [6] [2, 4]

Thanks to generics, it should be possible to make incrementing() (or whatever it ends up being called) non-applicable to a stream whose elements don't extend Comparable. The alternative incrementing(Comparator) would of course apply to all streams.

The symmetrical decrementing variant should probably be added together with this one.

@tginsberg
Copy link
Owner

tginsberg commented Feb 8, 2025

Hi! These two (incrementing and decrementing) are on my list, along with nonIncrementing (so, [1,1,2,1] -> [1,1,2],[1]) and nonDecrementing. I think that covers the full set of possibilities here? You're right that I think this would only apply to streams of elements that implement Comparable, or I suppose a version could be made that takes a Comparator. At any rate, on the list! :)

Not sure how happy I am with the names "nonIncrementing/nonDecrementing" even if they do describe what's happening. I'll take a walk and think about the name.

Thanks again @nipafx!

@tginsberg
Copy link
Owner

Hey @nipafx - I put in a PR which is my take on this. I did have one question about your example:

Stream.of(1, 3, 5, 6, 2, 4)
	.gather(incrementing()) // Stream<List<Integer>>
	.forEach(System.out::println); // [1, 3, 5] [6] [2, 4]

I think the result should be [1, 3, 5, 6] [2, 4], yes?

@nipafx
Copy link
Author

nipafx commented Feb 11, 2025

Oh, yes. 🙈 I wanted to show that a group may consist of just one element, but got the numbers wrong.

@tginsberg tginsberg added this to the v0.9.0 milestone Feb 12, 2025
tginsberg added a commit that referenced this issue Feb 12, 2025
…reasing/NonIncreasing/NonDecreasing (#93)

+ Group elements to Lists so long as they are increasing, decreasing, non-increasing, or non-decreasing
+ Ensure that streams are increasing, decreasing, non-increasing, or non-decreasing and fail otherwise
+ Filter streams to be increasing, decreasing, non-increasing, or non-decreasing
+ Group, Filter, and Ensure functions can operate on a stream of `Comparable` objects or caller can specify a `Comparator`
@tginsberg
Copy link
Owner

Merged something I'm happy with. I realize this is not fully documented in README, but I have plans to fully document each operation in an adoc-powered website so I'll save the work for that. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants