Skip to content

Creates a stream by repeatedly calling a lazily evaluated function#1139

Merged
fwbrasil merged 3 commits into
getkyo:mainfrom
vladpo:stream-repeat-present
Apr 13, 2025
Merged

Creates a stream by repeatedly calling a lazily evaluated function#1139
fwbrasil merged 3 commits into
getkyo:mainfrom
vladpo:stream-repeat-present

Conversation

@vladpo
Copy link
Copy Markdown
Contributor

@vladpo vladpo commented Apr 12, 2025

Creates a stream by repeatedly calling a lazily evaluated function, until the return is absent.

Problem

Stream emit an effect until some end state offers no more values.

Solution

repeatPresent is a factory method in the Stream class that creates a stream by repeatedly calling a function until it returns an absent value

  1. Create a stream from a function that may return values multiple times
  2. Automatically terminate the stream when the function returns an absent value
  3. Control the chunk size of emitted values for performance optimization

Notes

Copy link
Copy Markdown
Collaborator

@hearnadam hearnadam left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution! This definitely could be useful.

Emit.valueWith(c.take(_chunkSize))(Loop.continue(c.dropLeft(_chunkSize)))
v.map(seq => emitResized(seq, chunkSize))

private def emitResized[V](seq: Seq[V], chunkSize: Int)(using tag: Tag[Emit[Chunk[V]]], frame: Frame): Unit < Emit[Chunk[V]] =
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this is quite semantically correct for the repeat case as it may produce chunks like this:

given: repeatPresent(Chunk(1, 2, 3, 4), 3)
outcome: Stream(Chunk(1, 2, 3), Chunk(4), Chunk(1, 2, 3), Chunk(4)...)

In my opinion, it's reasonable for .rechunk to be used after the repeat operator, given rechunking will happen across suspension.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the reply. I've changed to calling rechunk.

}

"repeatPresent" - {
var i = 0
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please avoid mutability outside the scope of a single test if possible 🙏 it makes parallelizing/refactoring tests difficult.

Perhaps you can use the Var effect?

@vladpo vladpo force-pushed the stream-repeat-present branch from 56b6ed8 to 57b9653 Compare April 13, 2025 05:09
@fwbrasil
Copy link
Copy Markdown
Collaborator

thank you @vladpo!

@fwbrasil fwbrasil merged commit 029bda4 into getkyo:main Apr 13, 2025
3 checks passed
fwbrasil pushed a commit that referenced this pull request Apr 14, 2025
…1139)

Creates a stream by repeatedly calling a lazily evaluated function,
until the return is absent.

<!--
PRs require an approval from any of the core contributors, other than
the PR author.

Include this header if applicable:
Fixes #issue1, #issue2, ...
-->

### Problem
<!--
Explain here the context, and why you're making this change. What is the
problem you're trying to solve?
-->
Stream emit an effect until some end state offers no more values.

### Solution
<!--
Describe your solution. Focus on helping reviewers understand your
technical approach and implementation decisions.
-->
repeatPresent is a factory method in the Stream class that creates a
stream by repeatedly calling a function until it returns an absent value
1. Create a stream from a function that may return values multiple times
2. Automatically terminate the stream when the function returns an
absent value
3. Control the chunk size of emitted values for performance optimization


### Notes
<!--
Add any important additional information as bullet points, such as:
- Implementation details reviewers should know about
- Open questions and concerns
- Limitations
-->
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