fun main() {
val list = listOf(1, 2, 3, 4, 5)
if (list.isNotEmpty()) {
Nel(list[0], list.subList(1, list.lastIndex))
}
}
According to arrow documents, it seems be able to do it through Semigroup
or Monad binding
.
However, there is no code to replace List
with NonEmptyList
.
Is there a better way to replace List
withNonEmptyList
without using subList
?
There is a companion function fromList
that return an Option<NonEmptyList>
or if you are sure use fromListUnsafe
:
val list = listOf(1, 2, 3, 4, 5)
val nelistOption = NonEmptyList.fromList(list)
val nelist = NonEmptyList.fromListUnsafe(list)