Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
Remove the all value from Nel (#195)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>
Co-authored-by: Alberto Ballano <aballano@users.noreply.github.com>
Co-authored-by: Raúl Raja Martínez <raulraja@gmail.com>
Co-authored-by: Rachel M. Carmena <rachelcarmena@users.noreply.github.com>
  • Loading branch information
5 people authored Aug 15, 2020
1 parent 9331dd3 commit 5f7e957
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions arrow-core-data/src/main/kotlin/arrow/core/NonEmptyList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,27 @@ typealias Nel<A> = NonEmptyList<A>
*
*/
@higherkind
class NonEmptyList<out A> private constructor(
class NonEmptyList<out A>(
val head: A,
val tail: List<A>,
val all: List<A>
) : NonEmptyListOf<A>, Iterable<A> by all {
val tail: List<A>
) : NonEmptyListOf<A>, AbstractList<A>() {

constructor(head: A, tail: List<A>) : this(head, tail.toList(), listOf(head) + tail.toList())
private constructor(list: List<A>) : this(list[0], list.drop(1), list.toList())
private constructor(list: List<A>) : 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<A>
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<A> =
all
fun toList(): List<A> = listOf(head) + tail

inline fun <B> map(f: (A) -> B): NonEmptyList<B> =
NonEmptyList(f(head), tail.map(f))
Expand Down

0 comments on commit 5f7e957

Please sign in to comment.