Skip to main content
Version: 2.x

Operators

  1. Gen#zipWith โ€” Composes this generator with the specified generator to create a cartesian product of elements with the specified function:
Gen.elements("a", "b", "c").zipWith(Gen.elements("1", "2", "3"))(_ + _)
.runCollectN(5)
// Sample Output: List(b1, a2, c1, b1, b1)
  1. Gen#zip โ€” Composes this generator with the specified generator to create a cartesian product of elements.
Gen.elements("a", "b", "c").zip(Gen.elements("1", "2", "3"))
.runCollectN(5)
(Gen.elements("a", "b", "c") <*> Gen.elements("1", "2", "3"))
.runCollectN(5)

// Sample Output: List((a,3), (a,3), (c,3), (b,3), (c,2))
  1. Gen#collect โ€” Maps the values produced by this generator with the specified partial function, discarding any values the partial function is not defined at:
Gen.int(-10, +10)
.collect { case n if n % 2 == 0 => n }
.runCollectN(5)
.debug
// Smaple Output: List(-6, -8, -2, 4, -6)
  1. Gen#filter โ€” Filters the values produced by this generator, discarding any values that do not meet the specified predicate:
Gen.int(-10, +10).filter(_ % 2 == 0).runCollectN(5)
// Sample Output: List(-6, 10, 0, -8, 4)

Using filter can reduce test performance, especially if many values must be discarded. It is recommended to use combinators such as map and flatMap to create generators of the desired values instead:

Gen.int(-10, +10).map(_ * 2).runCollectN(5)
// Sample Output: List(2, 6, -6, 20, -14)

ZIO Chat

Hi, fellow coder! ๐ŸŒŸ Welcome to your ZIO adventure with the ZIO Chat Bot. ๐Ÿš€ Let's master functional Scala together. From referential transparency to concurrent prowess, I've got your back. Ready to dive in? Your questions, my insights โ€“ let's conquer ZIO! ๐Ÿ’ก๐Ÿค–