Skip to content

Commit

Permalink
Merge branch 'main' into arrow-2
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Oct 26, 2023
2 parents 402c280 + 1a2927b commit 24d7abc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
4 changes: 2 additions & 2 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,9 @@ public final class arrow/core/NonEmptySet : arrow/core/NonEmptyCollection, java/
public final class arrow/core/NonEmptySetKt {
public static final fun nonEmptySetOf (Ljava/lang/Object;[Ljava/lang/Object;)Ljava/util/Set;
public static final fun toNonEmptySetOrNone (Ljava/lang/Iterable;)Larrow/core/Option;
public static final fun toNonEmptySetOrNone (Ljava/util/Set;)Larrow/core/Option;
public static final synthetic fun toNonEmptySetOrNone (Ljava/util/Set;)Larrow/core/Option;
public static final fun toNonEmptySetOrNull (Ljava/lang/Iterable;)Ljava/util/Set;
public static final fun toNonEmptySetOrNull (Ljava/util/Set;)Ljava/util/Set;
public static final synthetic fun toNonEmptySetOrNull (Ljava/util/Set;)Ljava/util/Set;
}

public final class arrow/core/NonFatalKt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,11 @@ public inline fun <E, A, B> NonEmptyList<A>.mapOrAccumulate(
all.mapOrAccumulate(transform).map { requireNotNull(it.toNonEmptyListOrNull()) }

@JvmName("toNonEmptyListOrNull")
public fun <A> Iterable<A>.toNonEmptyListOrNull(): NonEmptyList<A>? =
toList().let { if (it.isEmpty()) null else NonEmptyList(it) }
public fun <A> Iterable<A>.toNonEmptyListOrNull(): NonEmptyList<A>? {
val iter = iterator()
if (!iter.hasNext()) return null
return NonEmptyList(iter.next(), Iterable { iter }.toList())
}

@JvmName("toNonEmptyListOrNone")
public fun <A> Iterable<A>.toNonEmptyListOrNone(): Option<NonEmptyList<A>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,19 @@ public value class NonEmptySet<out A> private constructor(
public fun <A> nonEmptySetOf(first: A, vararg rest: A): NonEmptySet<A> =
NonEmptySet(first, rest.toSet())

public fun <A> Iterable<A>.toNonEmptySetOrNull(): NonEmptySet<A>? =
firstOrNull()?.let { NonEmptySet(it, minus(it).toSet()) }
public fun <A> Iterable<A>.toNonEmptySetOrNull(): NonEmptySet<A>? {
val iter = iterator()
if (!iter.hasNext()) return null
return NonEmptySet(iter.next(), Iterable { iter }.toSet())
}

public fun <A> Iterable<A>.toNonEmptySetOrNone(): Option<NonEmptySet<A>> =
toNonEmptySetOrNull().toOption()

@Deprecated("Same as Iterable extension", level = DeprecationLevel.HIDDEN)
public fun <A> Set<A>.toNonEmptySetOrNull(): NonEmptySet<A>? =
firstOrNull()?.let { NonEmptySet(it, minus(it)) }
(this as Iterable<A>).toNonEmptySetOrNull()

@Deprecated("Same as Iterable extension", level = DeprecationLevel.HIDDEN)
public fun <A> Set<A>.toNonEmptySetOrNone(): Option<NonEmptySet<A>> =
toNonEmptySetOrNull().toOption()
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.kotest.assertions.withClue
import io.kotest.core.spec.style.StringSpec
import io.kotest.inspectors.shouldForAll
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
Expand All @@ -26,6 +27,16 @@ class NonEmptyListTest : StringSpec({
}
}

"iterable.toNonEmptyListOrNull should return null for an empty iterable" {
listOf<String>().toNonEmptyListOrNull().shouldBeNull()
}

"iterable.toNonEmptyListOrNull should work correctly when the iterable starts with or contains null" {
checkAll(Arb.nonEmptyList(Arb.int().orNull())) { nonEmptyList ->
nonEmptyList.all.toNonEmptyListOrNull().shouldNotBeNull() shouldBe nonEmptyList
}
}

"iterable.toNonEmptyListOrNone should round trip" {
checkAll(Arb.nonEmptyList(Arb.int())) { nonEmptyList ->
nonEmptyList.all.toNonEmptyListOrNone() shouldBe nonEmptyList.some()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package arrow.core
import arrow.core.test.nonEmptySet
import io.kotest.assertions.withClue
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.next
import io.kotest.property.arbitrary.orNull
import io.kotest.property.checkAll
import kotlin.test.Test
import kotlinx.coroutines.test.runTest
Expand All @@ -20,6 +22,16 @@ class NonEmptySetTest {
}
}

@Test fun iterableToNonEmptySetOrNullShouldReturnNullForEmptyIterable() = runTest {
listOf<String>().toNonEmptySetOrNull().shouldBeNull()
}

@Test fun iterableToNonEmptySetOrNullShouldReturnWorkWhenContainingNull() = runTest {
checkAll(Arb.nonEmptySet(Arb.int().orNull())) { nonEmptySet ->
nonEmptySet.toNonEmptySetOrNull().shouldNotBeNull() shouldBe nonEmptySet
}
}

@Test fun iterableToNonEmptySetOrNoneShouldRoundTrip() = runTest {
checkAll(Arb.nonEmptySet(Arb.int())) { nonEmptySet ->
nonEmptySet.toNonEmptySetOrNone() shouldBe nonEmptySet.some()
Expand Down

0 comments on commit 24d7abc

Please sign in to comment.