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

introduce iterable.toNonEmptyListOrNone() #2843

Merged
merged 3 commits into from
Nov 17, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,7 @@ public final class arrow/core/NonEmptyListKt {
public static final fun sequenceEither (Larrow/core/NonEmptyList;)Larrow/core/Either;
public static final fun sequenceOption (Larrow/core/NonEmptyList;)Larrow/core/Option;
public static final fun sequenceValidated (Larrow/core/NonEmptyList;Larrow/typeclasses/Semigroup;)Larrow/core/Validated;
public static final fun toNonEmptyListOrNone (Ljava/lang/Iterable;)Larrow/core/Option;
public static final fun toNonEmptyListOrNull (Ljava/lang/Iterable;)Larrow/core/NonEmptyList;
public static final fun traverse (Larrow/core/NonEmptyList;Larrow/typeclasses/Semigroup;Lkotlin/jvm/functions/Function1;)Larrow/core/Validated;
public static final fun traverse (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function1;)Larrow/core/Either;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,6 @@ public fun <A> NonEmptyList<Option<A>>.sequence(): Option<NonEmptyList<A>> =

public fun <A> Iterable<A>.toNonEmptyListOrNull(): NonEmptyList<A>? =
firstOrNull()?.let { NonEmptyList(it, drop(1)) }

public fun <A> Iterable<A>.toNonEmptyListOrNone(): Option<NonEmptyList<A>> =
toNonEmptyListOrNull().toOption()
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import arrow.core.test.laws.SemigroupLaws
import arrow.typeclasses.Semigroup
import io.kotest.assertions.withClue
import io.kotest.matchers.booleans.shouldBeTrue
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.property.Arb
import io.kotest.matchers.shouldBe
import io.kotest.property.arbitrary.boolean
Expand All @@ -17,6 +18,18 @@ class NonEmptyListTest : UnitSpec() {

testLaws(SemigroupLaws.laws(Semigroup.nonEmptyList(), Arb.nonEmptyList(Arb.int())))

"iterable.toNonEmptyListOrNull should round trip" {
checkAll(Arb.nonEmptyList(Arb.int())) { nonEmptyList ->
nonEmptyList.all.toNonEmptyListOrNull().shouldNotBeNull() shouldBe nonEmptyList
}
}

"iterable.toNonEmptyListOrNone should round trip" {
checkAll(Arb.nonEmptyList(Arb.int())) { nonEmptyList ->
nonEmptyList.all.toNonEmptyListOrNone() shouldBe nonEmptyList.some()
}
}

"traverse for Either stack-safe" {
// also verifies result order and execution order (l to r)
val acc = mutableListOf<Int>()
Expand Down