Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mijicd
Copy link
Member

@mijicd mijicd commented Jul 1, 2020

Closes #3872

@mijicd mijicd requested a review from iravid July 1, 2020 21:26
Copy link
Member

@iravid iravid left a comment

Choose a reason for hiding this comment

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

Two small tweaks and this is good to go 💪🏻

/**
* Creates a stream from [[java.io.Reader]].
*/
def fromReader(reader: => Reader): ZStream[Blocking, IOException, Char] =
Copy link
Member

@iravid iravid Jul 2, 2020

Choose a reason for hiding this comment

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

Ahoy! Let's make this similar to fromInputStream. Important points:

  • Chunking: there's a read variant that reads into a Char array and you can use repeatEffectOptionChunk
  • The by-name reader needs to be captured in value (otherwise it gets re-initialized every time)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, good points. I was overeager to make this super simple :). Updated.

e => ZIO.fail(Some(e)),
r => if (r == -1) ZIO.fail(None) else ZIO.succeed(r.toChar)
)
def fromReader(reader: => Reader, chunkSize: Int = ZStream.DefaultChunkSize): ZStream[Blocking, IOException, Char] =
Copy link
Member

Choose a reason for hiding this comment

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

I liked your previous approach with repeatEffectOption better :-) How about:
ZStream.fromEffect(reader).flatMap { capturedReader => ZStream.repeatEffectOptionChunk(...)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha, merged it, but dropped the exceptions used in first try.

Copy link
Member Author

Choose a reason for hiding this comment

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

Btw. should I do the same for fromInputStream?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah why not. Can you add a few tests for fromReader?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@iravid iravid left a comment

Choose a reason for hiding this comment

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

Superb, thank you!

@mijicd mijicd merged commit 9fe75e0 into zio:master Jul 2, 2020
@mijicd mijicd deleted the 3872 branch July 2, 2020 08:32
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.

Add ZStream.fromReader

2 participants