diff --git a/arrow-core-data/src/main/kotlin/arrow/core/NonEmptyList.kt b/arrow-core-data/src/main/kotlin/arrow/core/NonEmptyList.kt
index 68e909d62..6409421cc 100644
--- a/arrow-core-data/src/main/kotlin/arrow/core/NonEmptyList.kt
+++ b/arrow-core-data/src/main/kotlin/arrow/core/NonEmptyList.kt
@@ -194,30 +194,27 @@ typealias Nel = NonEmptyList
*
*/
@higherkind
-class NonEmptyList private constructor(
+class NonEmptyList(
val head: A,
- val tail: List,
- val all: List
-) : NonEmptyListOf, Iterable by all {
+ val tail: List
+) : NonEmptyListOf, AbstractList() {
- constructor(head: A, tail: List) : this(head, tail.toList(), listOf(head) + tail.toList())
- private constructor(list: List) : this(list[0], list.drop(1), list.toList())
+ private constructor(list: List) : this(list[0], list.drop(1))
- val size: Int =
- all.size
+ override val size: Int =
+ 1 + tail.size
- fun contains(element: @UnsafeVariance A): Boolean =
- (head == element) || element in tail
+ val all: List
+ get() = toList()
- fun containsAll(elements: Collection<@UnsafeVariance A>): Boolean =
- elements.all(this::contains)
+ override operator fun get(index: Int): A {
+ if (index < 0 || index >= size) throw IndexOutOfBoundsException("$index is not in 1..${size - 1}")
+ return if (index == 0) head else tail[index - 1]
+ }
- @Suppress("FunctionOnlyReturningConstant")
- fun isEmpty(): Boolean =
- false
+ override fun isEmpty(): Boolean = false
- fun toList(): List =
- all
+ fun toList(): List = listOf(head) + tail
inline fun map(f: (A) -> B): NonEmptyList =
NonEmptyList(f(head), tail.map(f))