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

Interleave with function #87

Open
nipafx opened this issue Feb 6, 2025 · 2 comments
Open

Interleave with function #87

nipafx opened this issue Feb 6, 2025 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@nipafx
Copy link

nipafx commented Feb 6, 2025

I had an interleave use case where the interleaved elements were computed from the previous element. The use looked as follows:

Stream.of(0, 2, 4)
	// provide a `UnaryOperator<Integer>`
	.gather(interleave(number -> number + 1))
	.forEach(System.out::println); // 0, 1, 2, 3, 4, 5

There are other use cases in this realm, though:

  • don't always interleave (provide Function<T, Optional<T>>)
  • interleave potentially many elements (provide Function<T, List<T>>)
  • receive all elements since previous interleave (i.e. if the last function call didn't return an element to interleave, provide both former stream elements as input; UnaryOperator<List<T>>)

Each of these use cases includes the previous ones, so allowing just the last one would catch three birds with one gatherer (I couldn't help myself) but with an API that is more annoying that needed for the simpler (and probably more common) use cases.

@tginsberg
Copy link
Owner

Interesting idea! I implemented something like this (mapWithOriginal) and parked it in a feature branch I haven't pushed, but the concept is really similar. I like your implementation, and I appreciate the detailed analysis (and the joke!). Let me look at it, this sounds like a fun idea. What kind of problem were you solving where you needed this, if I might ask?

@tginsberg tginsberg self-assigned this Feb 6, 2025
@tginsberg tginsberg added the enhancement New feature or request label Feb 6, 2025
@nipafx
Copy link
Author

nipafx commented Feb 7, 2025

What kind of problem were you solving where you needed this, if I might ask?

Funny you should ask - I can't remember details and I can't find the calls site or implementation, either. 😅 It may have fallen victim to a refactoring and consequent removal of unused code. But if I remember correctly, it wasn't something simple like in the example above but a "complex" object being constructed.

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