Skip to content

Commit

Permalink
Add mapOrAccumulate extension in RaiseAccumulate (arrow-kt#3086)
Browse files Browse the repository at this point in the history
  • Loading branch information
nomisRev authored and kyay10 committed Aug 18, 2023
1 parent 390104a commit 2c7f558
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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()
}
})

0 comments on commit 2c7f558

Please sign in to comment.