From c504b8d1ed19bdfce275af27f0546755a213a980 Mon Sep 17 00:00:00 2001 From: Dorian Sarnowski Date: Sat, 1 Jan 2022 16:58:24 +0100 Subject: [PATCH 1/2] max and min methods for NonEmptyList --- .../kotlin/arrow/core/NonEmptyList.kt | 12 ++++++ .../kotlin/arrow/core/NonEmptyListTest.kt | 42 ++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt index 26adcb8c9bc..cb9a420f753 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt @@ -383,6 +383,12 @@ public class NonEmptyList( 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 > minBy(selector: (A) -> B): A = + minByOrNull(selector) ?: head + + public inline fun > maxBy(selector: (A) -> B): A = + maxByOrNull(selector) ?: head } public fun nonEmptyListOf(head: A, vararg t: A): NonEmptyList = @@ -397,6 +403,12 @@ public operator fun > NonEmptyList.compareTo(other: NonEmpt public fun NonEmptyList>.flatten(): NonEmptyList = this.flatMap(::identity) +public inline fun > NonEmptyList.min(): T = + minOrNull() ?: head + +public inline fun > NonEmptyList.max(): T = + maxOrNull() ?: head + public fun NonEmptyList>.unzip(): Pair, NonEmptyList> = this.unzip(::identity) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptyListTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptyListTest.kt index a14d8893a07..bbbb76a1204 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptyListTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptyListTest.kt @@ -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 @@ -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 + } + } } } From 0fd10b3b30d20caf87e0fd0db69d8fb2205edd6f Mon Sep 17 00:00:00 2001 From: Alejandro Serrano Date: Wed, 6 Jul 2022 18:18:46 +0200 Subject: [PATCH 2/2] Use !! and perform apiDump --- arrow-libs/core/arrow-core/api/arrow-core.api | 4 ++++ .../commonMain/kotlin/arrow/core/NonEmptyList.kt | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 54eaa80bfe4..7407e7283cc 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -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; diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt index 087d59e89a6..80e58e68eb1 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt @@ -382,12 +382,6 @@ public class NonEmptyList( 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 > minBy(selector: (A) -> B): A = - minByOrNull(selector) ?: head - - public inline fun > maxBy(selector: (A) -> B): A = - maxByOrNull(selector) ?: head } public fun nonEmptyListOf(head: A, vararg t: A): NonEmptyList = @@ -402,11 +396,17 @@ public operator fun > NonEmptyList.compareTo(other: NonEmpt public fun NonEmptyList>.flatten(): NonEmptyList = this.flatMap(::identity) +public inline fun > NonEmptyList.minBy(selector: (A) -> B): A = + minByOrNull(selector)!! + +public inline fun > NonEmptyList.maxBy(selector: (A) -> B): A = + maxByOrNull(selector)!! + public inline fun > NonEmptyList.min(): T = - minOrNull() ?: head + minOrNull()!! public inline fun > NonEmptyList.max(): T = - maxOrNull() ?: head + maxOrNull()!! public fun NonEmptyList>.unzip(): Pair, NonEmptyList> = this.unzip(::identity)