NonEmptyChunk
A NonEmptyChunk[A] is a Chunk that holds at least one element. The type says so, which makes two things safe. A function can require at least one input (def average(xs: NonEmptyChunk[Double])), and the caller cannot forget the empty case. And head, last and reduce, which throw on an empty Chunk, always return a value. ZIO uses it where an empty collection makes no sense, for example in Config.nonEmptyChunkOf and in the errors that ZIO.validate accumulates.
Creating a NonEmptyChunkโ
When we know the elements, NonEmptyChunk.apply takes one element followed by any number of others, and NonEmptyChunk.single takes exactly one. Both return a NonEmptyChunk directly:
import zio._
val numbers: NonEmptyChunk[Int] = NonEmptyChunk(1, 2, 3)
// numbers: NonEmptyChunk[Int] = NonEmptyChunk(1, 2, 3)
val one: NonEmptyChunk[String] = NonEmptyChunk.single("only")
// one: NonEmptyChunk[String] = NonEmptyChunk(only)
When the elements come from a collection that may be empty, the constructors return an Option: NonEmptyChunk.fromChunk and NonEmptyChunk.fromIterableOption return None for an empty input. NonEmptyChunk.fromIterable takes the first element separately, and NonEmptyChunk.fromCons takes the non-empty case :: of a List, so neither needs an Option:
NonEmptyChunk.fromChunk(Chunk(4, 5))
// res0: Option[NonEmptyChunk[Int]] = Some(value = NonEmptyChunk(4, 5))
NonEmptyChunk.fromChunk(Chunk.empty[Int])
// res1: Option[NonEmptyChunk[Int]] = None
NonEmptyChunk.fromIterable(0, List(1, 2))
// res2: NonEmptyChunk[Int] = NonEmptyChunk(0, 1, 2)
Operations That Keep It Non-Emptyโ
Operations that cannot remove elements return a NonEmptyChunk again: map, flatMap (with a function that returns a NonEmptyChunk), zip, zipWith, zipWithIndex, sorted, sortBy, reverse, distinct, prepended/+:, appended/:+ and ++. Operations that need an element are total:
numbers.map(_ * 10)
// res3: NonEmptyChunk[Int] = NonEmptyChunk(10, 20, 30)
numbers.head
// res4: Int = 1
numbers.last
// res5: Int = 3
numbers.reduce(_ + _)
// res6: Int = 6
NonEmptyChunk#reduceMapLeft and NonEmptyChunk#reduceMapRight map the first element with one function and fold the rest with another, so the result type can differ from the element type without a starting value:
numbers.reduceMapLeft(_.toString)(_ + "-" + _)
// res7: String = "1-2-3"
NonEmptyChunk#groupBy and NonEmptyChunk#groupMap return a Map whose values are NonEmptyChunks too, because every group has at least one element.
Operations That Return a Chunkโ
filter, filterNot, collect, tail, init and the other operations that may drop elements return a plain Chunk, since the result may be empty. NonEmptyChunk#toChunk converts explicitly, and an implicit conversion turns a NonEmptyChunk into a Chunk wherever a Chunk is expected:
numbers.filter(_ > 1)
// res8: Chunk[Int] = IndexedSeq(2, 3)
val plain: Chunk[Int] = numbers
// plain: Chunk[Int] = IndexedSeq(1, 2, 3)
NonEmptySeq and NonEmptyOpsโ
The operations shared by non-empty collections are declared in two traits: NonEmptyOps lists the ones that work on any non-empty collection, and NonEmptySeq adds those that keep at least one element and return the non-empty type. NonEmptyChunk implements both. You rarely use these traits directly; they matter only when writing code that is generic over non-empty collections.
See Alsoโ
- Chunk โ The general immutable, array-backed sequence that
NonEmptyChunkwraps. - Error Accumulation โ
ZIO.validateand related operators, which collect errors in a non-empty collection.