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

Skip to content

Conversation

@mschuwalow
Copy link
Member

part of #3865

@mschuwalow mschuwalow requested a review from iravid as a code owner July 4, 2020 12:20
def last[O]: ZTransducer[Any, Nothing, O, Option[O]] =
foldLeft[O, Option[O]](Option.empty[O])((_, a) => Some(a))

def magicSequence[R, E, I, O](n: Int)(f: Chunk[I] => ZTransducer[R, E, I, O]): ZTransducer[R, E, I, O] =
Copy link
Member Author

@mschuwalow mschuwalow Jul 4, 2020

Choose a reason for hiding this comment

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

I think this can be more general in that we could have a combinator that "decides" on a transducer after the first element has been emitted and is reset on a push(None).
Should we add something like this? Signature would be:

ZTransducer[R, E, I, O] {
  def replace[R1 <: R, E1 >: E, I1 <: I, O1](f: O => ZTransducer[R1, E1, I1, O1]): ZTransducer[R1, E1, I1, O1]
}

Copy link
Member

Choose a reason for hiding this comment

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

This is kind of similar to ZStream#peel but in a transducer form. The infix form would be self >>> ZStream.magicSequence no?

Either way would be great if we can just find a better name before merging. Something around branching, headers?

Copy link
Member Author

Choose a reason for hiding this comment

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

True, so we probably don't need an additional combinator.

What about branchAfter for the name? As in "after reading n values, branch"

ZRef.makeManaged[Option[Byte]](None).map { stateRef =>
{
case None =>
// should we raise an error here if we have leftovers?
Copy link
Member Author

Choose a reason for hiding this comment

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

What should we do here? If we do not fail the transducer we will lose data as there is no way to decode the orphaned byte

Copy link
Member

Choose a reason for hiding this comment

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

With the UTF-8 decoding, if the stream ends (which is what None signifies) in the middle of 2/3/4 byte sequence, we just defer to the underlying charset decoder (which if I recall correctly just drops the malformed bytes). I'm fine with adopting the same approach here.

In the future, the None case will really signal the end of stream and we will stop allowing transducers to be restarted during stream processing. This will be done by separating transducers for which restarts do make sense (all the folds basically) to a separate abstraction (ZAggregator or something like that).

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, I see. Thanks for the explanation 👍

@iravid
Copy link
Member

iravid commented Jul 9, 2020 via email

@iravid iravid merged commit 5ba6ac8 into zio:master Jul 15, 2020
@iravid
Copy link
Member

iravid commented Jul 15, 2020

Thank you @mschuwalow!

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.

2 participants