Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package arrow.core

import arrow.core.test.either
import arrow.core.test.ior
import arrow.core.test.nonEmptyList
import arrow.core.test.option
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.boolean
Expand All @@ -23,36 +23,14 @@ class IterableTest {
@Test
fun flattenOrAccumulateCombine() = runTest(timeout = 30.seconds) {
checkAll(Arb.list(Arb.either(Arb.string(maxSize = 10), Arb.int()), range = 0..20)) { list ->
val expected =
if (list.any { it.isLeft() }) {
list
.filterIsInstance<Either.Left<String>>()
.fold("") { acc, either -> "$acc${either.value}" }
.left()
} else {
list.filterIsInstance<Either.Right<Int>>().map { it.value }.right()
}

list.flattenOrAccumulate(String::plus) shouldBe expected
list.flattenOrAccumulate(String::plus) shouldBe list.simpleFlattenOrAccumulate().mapLeft { it.reduce(String::plus) }
}
}

@Test
fun flattenOrAccumulateOk() = runTest {
checkAll(Arb.list(Arb.either(Arb.int(), Arb.int()), range = 0..20)) { list ->
val expected =
if (list.any { it.isLeft() }) {
list
.filterIsInstance<Either.Left<Int>>()
.map { it.value }
.toNonEmptyListOrNull()
.shouldNotBeNull()
.left()
} else {
list.filterIsInstance<Either.Right<Int>>().map { it.value }.right()
}

list.flattenOrAccumulate() shouldBe expected
list.flattenOrAccumulate() shouldBe list.simpleFlattenOrAccumulate()
}
}

Expand All @@ -76,11 +54,7 @@ class IterableTest {
checkAll(Arb.list(Arb.int(), range = 0..20)) { ints ->
val res = ints.mapOrAccumulate { i -> if (predicate(i)) i else raise(i) }

val expected: Either<NonEmptyList<Int>, List<Int>> =
ints.filterNot(::predicate).toNonEmptyListOrNull()?.left()
?: ints.filter(::predicate).right()

res shouldBe expected
res shouldBe (ints.filterNot(::predicate).toNonEmptyListOrNull()?.left() ?: ints.right())
}
}

Expand Down Expand Up @@ -475,28 +449,13 @@ class IterableTest {
checkAll(
Arb.list(
Arb.either(
Arb.list(Arb.string(0..5), 0..5),
Arb.nonEmptyList(Arb.string(0..5), 0..5),
Arb.int(),
),
0..10,
),
) { list ->
val nelslist = list.mapNotNull { it.withLeftListAsNelOrNull() }

val expected =
if (nelslist.any { it.isLeft() }) {
nelslist
.filterIsInstance<Either.Left<NonEmptyList<String>>>()
.map { it.value }
.flatten()
.toNonEmptyListOrNull()
.shouldNotBeNull()
.left()
} else {
nelslist.filterIsInstance<Either.Right<Int>>().map { it.value }.right()
}

nelslist.flattenOrAccumulate() shouldBe expected
list.flattenOrAccumulate() shouldBe list.simpleFlattenOrAccumulate().mapLeft { it.flatten() }
}
}

Expand All @@ -505,29 +464,13 @@ class IterableTest {
checkAll(
Arb.list(
Arb.either(
Arb.list(Arb.string(0..5), 0..5),
Arb.nonEmptyList(Arb.string(0..5), 0..5),
Arb.int(),
),
0..10,
),
) { list ->
val nelslist = list.mapNotNull { it.withLeftListAsNelOrNull() }

val expected =
if (nelslist.any { it.isLeft() }) {
nelslist
.filterIsInstance<Either.Left<NonEmptyList<String>>>()
.fold("") { accA, either ->
"$accA${
either.value.fold("") {accB, entry -> "$accB$entry"}
}"
}
.left()
} else {
nelslist.filterIsInstance<Either.Right<Int>>().map { it.value }.right()
}

nelslist.flattenOrAccumulate(String::plus) shouldBe expected
list.flattenOrAccumulate(String::plus) shouldBe list.simpleFlattenOrAccumulate().mapLeft { it.flatten().reduce(String::plus) }
}
}

Expand Down Expand Up @@ -623,10 +566,7 @@ class IterableTest {
}
}

private fun <E, A> Either<List<E>, A>.withLeftListAsNelOrNull(): Either<NonEmptyList<E>, A>? = when (this) {
is Either.Left -> {
value.toNonEmptyListOrNull()?.left()
}

is Either.Right -> value.right()
private fun <A, B> List<Either<A, B>>.simpleFlattenOrAccumulate(): Either<NonEmptyList<A>, List<B>> {
val (lefts, rights) = this.separateEither()
return lefts.toNonEmptyListOrNull()?.left() ?: rights.right()
}
Loading
Loading