Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

max and min methods for NonEmptyList #2622

Merged
merged 7 commits into from Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -383,6 +383,12 @@ public class NonEmptyList<out A>(
map(head, b.head, c.head, d.head, e.head, f.head, g.head, h.head, i.head, j.head),
tail.zip(b.tail, c.tail, d.tail, e.tail, f.tail, g.tail, h.tail, i.tail, j.tail, map)
)

public inline fun <B : Comparable<B>> minBy(selector: (A) -> B): A =
minByOrNull(selector) ?: head
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont need ?: head, just minByOrNull(selector)!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!! should indeed be safe here, I think it results in a faster binary but not 100% sure actually.


public inline fun <B : Comparable<B>> maxBy(selector: (A) -> B): A =
maxByOrNull(selector) ?: head
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

}

public fun <A> nonEmptyListOf(head: A, vararg t: A): NonEmptyList<A> =
Expand All @@ -397,6 +403,12 @@ public operator fun <A : Comparable<A>> NonEmptyList<A>.compareTo(other: NonEmpt
public fun <A> NonEmptyList<NonEmptyList<A>>.flatten(): NonEmptyList<A> =
this.flatMap(::identity)

public inline fun <T : Comparable<T>> NonEmptyList<T>.min(): T =
minOrNull() ?: head
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


public inline fun <T : Comparable<T>> NonEmptyList<T>.max(): T =
maxOrNull() ?: head
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


public fun <A, B> NonEmptyList<Pair<A, B>>.unzip(): Pair<NonEmptyList<A>, NonEmptyList<B>> =
this.unzip(::identity)

Expand Down
Expand Up @@ -4,7 +4,6 @@ import arrow.core.test.UnitSpec
import arrow.core.test.laws.SemigroupLaws
import arrow.typeclasses.Semigroup
import io.kotest.property.Arb
import io.kotest.property.checkAll
import io.kotest.matchers.shouldBe
import io.kotest.property.arbitrary.boolean
import io.kotest.property.arbitrary.int
Expand Down Expand Up @@ -268,5 +267,46 @@ class NonEmptyListTest : UnitSpec() {
result shouldBe expected
}
}

"max element" {
checkAll(
Arb.nonEmptyList(Arb.int())
) { a ->
val result = a.max()
val expected = a.maxOrNull()
result shouldBe expected
}
}

"maxBy element" {
checkAll(
Arb.nonEmptyList(Arb.int())
) { a ->
val result = a.maxBy(::identity)
val expected = a.maxByOrNull(::identity)
result shouldBe expected
}
}

"min element" {
checkAll(
Arb.nonEmptyList(Arb.int())
) { a ->
val result = a.min()
val expected = a.minOrNull()
result shouldBe expected
}
}


"minBy element" {
checkAll(
Arb.nonEmptyList(Arb.int())
) { a ->
val result = a.minBy(::identity)
val expected = a.minByOrNull(::identity)
result shouldBe expected
}
}
}
}