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

Add mapOrAccumulate extension in RaiseAccumulate #3086

Merged
merged 4 commits into from
Jul 10, 2023
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
3 changes: 3 additions & 0 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3455,6 +3455,9 @@ public final class arrow/core/raise/Raise$DefaultImpls {

public class arrow/core/raise/RaiseAccumulate : arrow/core/raise/Raise {
public fun <init> (Larrow/core/raise/Raise;)V
public final fun _mapOrAccumulate (Larrow/core/NonEmptyList;Lkotlin/jvm/functions/Function2;)Larrow/core/NonEmptyList;
public final fun _mapOrAccumulate (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public final fun _mapOrAccumulate (Ljava/util/Set;Lkotlin/jvm/functions/Function2;)Ljava/util/Set;
public fun attempt (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun bind (Larrow/core/Either;)Ljava/lang/Object;
public fun bind (Larrow/core/Validated;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,27 @@ public open class RaiseAccumulate<Error>(
transform: RaiseAccumulate<Error>.(A) -> B
): NonEmptySet<B> = raise.mapOrAccumulate(this, transform)

@RaiseDSL
@JvmName("_mapOrAccumulate")
public inline fun <A, B> mapOrAccumulate(
iterable: Iterable<A>,
transform: RaiseAccumulate<Error>.(A) -> B
): List<B> = raise.mapOrAccumulate(iterable, transform)

@RaiseDSL
@JvmName("_mapOrAccumulate")
public inline fun <A, B> mapOrAccumulate(
list: NonEmptyList<A>,
transform: RaiseAccumulate<Error>.(A) -> B
): NonEmptyList<B> = raise.mapOrAccumulate(list, transform)

@RaiseDSL
@JvmName("_mapOrAccumulate")
public inline fun <A, B> mapOrAccumulate(
set: NonEmptySet<A>,
transform: RaiseAccumulate<Error>.(A) -> B
): NonEmptySet<B> = raise.mapOrAccumulate(set, transform)

@RaiseDSL
override fun <A> Iterable<Either<Error, A>>.bindAll(): List<A> =
mapOrAccumulate { it.bind() }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package arrow.core.raise

import arrow.core.NonEmptyList
import arrow.core.left
import arrow.core.nonEmptyListOf
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class RaiseAccumulateSpec : StringSpec({
"RaiseAccumulate takes precedence over extension function" {
either<NonEmptyList<String>, Int> {
zipOrAccumulate(
{ ensure(false) { "false" } },
{ mapOrAccumulate(1..2) { ensure(false) { "$it: IsFalse" } } }
) { _, _ -> 1 }
} shouldBe nonEmptyListOf("false", "1: IsFalse", "2: IsFalse").left()
}
})