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 Stream.evals, evalSeq #1582

Merged
merged 2 commits into from
Sep 6, 2019
Merged

Conversation

kubukoz
Copy link
Member

@kubukoz kubukoz commented Sep 5, 2019

Closes #1571.

@kubukoz
Copy link
Member Author

kubukoz commented Sep 5, 2019

I can revert the changes scalafmt has made, if it'll make anything easier, but it's a separate commit anyway so should be easy for review.

@@ -3068,6 +3141,14 @@ object Stream extends StreamLowPriority {
def evalUnChunk[F[_], O](fo: F[Chunk[O]]): Stream[F, O] =
fromFreeC(Algebra.eval(fo).flatMap(Algebra.output))

/** Like `eval`, but lifts a foldable structure. **/
def evals[F[_], S[_]: Foldable, O](fo: F[S[O]]): Stream[F, O] =
eval(fo).flatMap(_.foldMap(Stream.emit))
Copy link
Member Author

Choose a reason for hiding this comment

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

I went with foldMap, but also considered Stream.emits(foo.toList). Should I benchmark whether one would have a significant performance improvement over the other?

@mpilquist mpilquist self-assigned this Sep 5, 2019
@mpilquist mpilquist self-requested a review September 5, 2019 21:59
@mpilquist mpilquist removed their assignment Sep 5, 2019
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 great! I like the idea of using Foldable and foldMap.

@kubukoz
Copy link
Member Author

kubukoz commented Sep 5, 2019

What about the Seq variant? Maybe I should use fromIterator in it. I'll give that a try and maybe do a quick benchmark.

@mpilquist
Copy link
Member

Okay cool -- I suspect the emits will version will be faster, at least for lists. Though all I'm certain of is that benchmarks always surprise me. :)

@mpilquist
Copy link
Member

I'm going to merge as is for now, feel free to open a new PR if benchmark results change implementation. I'm getting the same Scalafmt results and want to avoid conflicts.

@mpilquist mpilquist merged commit 29789ca into typelevel:series/1.1 Sep 6, 2019
@mpilquist mpilquist modified the milestones: 1.1.0-M2, 1.1.0 Sep 6, 2019
@kubukoz kubukoz deleted the stream-evals branch September 6, 2019 11:56
@kubukoz
Copy link
Member Author

kubukoz commented Sep 7, 2019

foldMap seems to be consistently slower (by at least one order of magnitude) than emits when tested with List, scala.Stream and Chain. I'll replace it with emits in the subsequent PR ;)

The iterator solution is also massively slower than emits, so I'll leave evalSeq as it is for now.

If anyone wants to optimize for a specific collection/size, they can always use the more low-level operators themselves.

@kubukoz kubukoz mentioned this pull request Sep 7, 2019
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.

Allow creating stream from sequence in effect
2 participants