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

Skip to content

Conversation

@fagossa
Copy link
Contributor

@fagossa fagossa commented May 2, 2020

This is an implementation for intersperse (#3440).

For example:

Stream(1, 2, 3, 4).map(_.toString).intersperse("@")
//  "1", "@", "2", "@", "3", "@", "4"

@CLAassistant
Copy link

CLAassistant commented May 2, 2020

CLA assistant check
All committers have signed the CLA.

@fagossa fagossa mentioned this pull request May 2, 2020
@fagossa fagossa changed the title Implement intersperse in ZStream WIP: Implement intersperse in ZStream May 2, 2020
@fagossa fagossa changed the title WIP: Implement intersperse in ZStream WIP: Implement ZStream#intersperse May 2, 2020
@regiskuckaertz
Copy link
Member

Let's make this a transducer instead, right? Then it's just a fold that inserts the element every even one:

state.modify { flag =>
  os.foldRight(Nil -> flag) { 
    case (o, (out, true)) => (a :: o :: out) -> false
    case (o, (out, false)) => (o :: out) -> true
  }
}

@fagossa fagossa marked this pull request as ready for review May 6, 2020 20:19
@fagossa fagossa requested a review from iravid as a code owner May 6, 2020 20:19
@fagossa fagossa changed the title WIP: Implement ZStream#intersperse Implement ZStream#intersperse May 6, 2020
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.

Looks great. One small comment.

/**
* Intersperse stream with provided element similar to <code>List.mkString</code>.
*/
final def intersperse[O1 >: O](middle: O1): ZStream[R, E, O1] =
Copy link
Member

Choose a reason for hiding this comment

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

@fagossa Could we add a variant that also adds start/end markers? This could simply be ZStream(start) ++ intersperse(middle) ++ ZStream(end).

Copy link
Contributor 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.

Awesome contribution @fagossa. Super useful operator. Thank you!

@iravid iravid merged commit e13870b into zio:master May 8, 2020
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.

4 participants