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

Iterator Handling in IO Package #978

Merged
merged 4 commits into from
Nov 21, 2017
Merged

Iterator Handling in IO Package #978

merged 4 commits into from
Nov 21, 2017

Conversation

ChristopherDavenport
Copy link
Member

Fixes #971

If you are interested in this being implemented. It used to be present in scalaz-stream, and is a useful piece of functionality. If there is a problem in this implementation I would like to know so I can avoid it.

/**
* Reads all the values of the iterator into a Stream.
*/
def readIterator[F[_], A](iterator: Iterator[A])(implicit F: Effect[F]): Stream[F, A] = {
Copy link
Member

Choose a reason for hiding this comment

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

I think we can move this method to Stream companion as Stream.fromIterator -- the effect constraint can be loosened to Sync.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can make this a bit more concise with:

  def readIterator2[F[_], A](iterator: Iterator[A])(implicit F: Sync[F]): Stream[F, A] =
    Stream.unfoldEval(iterator)(i => F.delay(i.hasNext.guard[Option].as(i.next -> i)))

it's a style choice though, so up to you

/**
* Brackets Resource Acquisition for an Iterator
*/
def readBracketedIterator[F[_]: Effect, R, O](acquire: F[R])
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced we need this method. If we have Stream.fromIterator, I think putting that together with Stream.bracket is simple enough.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm also a bit unconvinced we need this, especially because the user still has to do some massaging to get an R => F[Iterator[O]] (the wrapping in F), at which point one could use bracket directly

/**
* Reads all the values of the iterator into a Stream.
*/
def readIterator[F[_], A](iterator: Iterator[A])(implicit F: Effect[F]): Stream[F, A] = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can make this a bit more concise with:

  def readIterator2[F[_], A](iterator: Iterator[A])(implicit F: Sync[F]): Stream[F, A] =
    Stream.unfoldEval(iterator)(i => F.delay(i.hasNext.guard[Option].as(i.next -> i)))

it's a style choice though, so up to you

Moves package to Stream companion object, removes the bracketed component, in favor of
using bracket directly, and switches to guard notation
@mpilquist mpilquist merged commit 6b717fb into typelevel:series/0.10 Nov 21, 2017
@ChristopherDavenport ChristopherDavenport deleted the iteratorAddition branch November 21, 2017 15:49
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