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 all commits
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
4 changes: 4 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Expand Up @@ -743,6 +743,10 @@ public final class arrow/core/NonEmptyList$Companion {
public final class arrow/core/NonEmptyListKt {
public static final fun compareTo (Larrow/core/NonEmptyList;Larrow/core/NonEmptyList;)I
public static final fun flatten (Larrow/core/NonEmptyList;)Larrow/core/NonEmptyList;
public static final fun max (Larrow/core/NonEmptyList;)Ljava/lang/Comparable;
public static final fun maxBy (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun min (Larrow/core/NonEmptyList;)Ljava/lang/Comparable;
public static final fun minBy (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun nel (Ljava/lang/Object;)Larrow/core/NonEmptyList;
public static final fun nonEmptyListOf (Ljava/lang/Object;[Ljava/lang/Object;)Larrow/core/NonEmptyList;
public static final fun sequence (Larrow/core/NonEmptyList;)Larrow/core/Either;
Expand Down
Expand Up @@ -396,6 +396,18 @@ 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 <A, B : Comparable<B>> NonEmptyList<A>.minBy(selector: (A) -> B): A =
minByOrNull(selector)!!

public inline fun <A, B : Comparable<B>> NonEmptyList<A>.maxBy(selector: (A) -> B): A =
maxByOrNull(selector)!!

public inline fun <T : Comparable<T>> NonEmptyList<T>.min(): T =
minOrNull()!!

public inline fun <T : Comparable<T>> NonEmptyList<T>.max(): T =
maxOrNull()!!

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

Expand Down
Expand Up @@ -267,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
}
}
}
}