Skip to main content

Non-empty collections

Nullable types give us the ability to have zero or one appearance of a value, collections allow us to have any number of them. However, in some scenarios, we need to work with collections that should contain at least one element. Arrow provides both NonEmptyList and NonEmptySet.

One concrete example is given by error accumulation. A type like Either<List<Problem>, Result> may take us to the weird case in which we end with Left, but we have no Problems in the List. To avoid this issue, Arrow makes mapOrAccumulate and zipOrAccumulate return Either<NonEmptyList<Problem>, Result> instead.

The API of non-empty collections follows the conventions from kotlin.collections. On top of that, some types become stronger to ensure or keep track of the non-empty nature of the collection.

  • nonEmptyListOf and nonEmptySetOf require at least one value as argument.
  • map, zip, and similar operations which respect the size of the arguments return non-empty collections.
  • Concatenation where one of the arguments is non-empty ensures that the result is also non-empty.