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

'accumulate' for Raise #2872

Merged
merged 10 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -2557,6 +2557,7 @@ public final class arrow/core/continuations/Raise$DefaultImpls {
}

public final class arrow/core/continuations/RaiseKt {
public static final fun accumulate (Larrow/core/continuations/Raise;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public static final fun catch (Larrow/core/continuations/Raise;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
public static final fun ensure (Larrow/core/continuations/Raise;ZLkotlin/jvm/functions/Function0;)V
public static final fun ensureNotNull (Larrow/core/continuations/Raise;Ljava/lang/Object;Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package arrow.core.continuations

import arrow.core.Either
import arrow.core.NonEmptyList
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
Expand Down Expand Up @@ -247,3 +248,23 @@ public inline fun <R, B : Any> Raise<R>.ensureNotNull(value: B?, raise: () -> R)
contract { returns() implies (value != null) }
return value ?: raise(raise())
}

/**
* Accumulate the errors obtained by executing the [block]
* over every element of [list].
*/
@EffectDSL
public fun <R, A, B> Raise<NonEmptyList<R>>.accumulate(
serras marked this conversation as resolved.
Show resolved Hide resolved
list: Iterable<A>,
block: Raise<R>.(A) -> B
): List<B> {
val errors = mutableListOf<R>()
val results = mutableListOf<B>()
list.forEach {
fold<R, B, Unit>({ block(it) }, { errors.add(it) }, { results.add(it) })
}
if (errors.isNotEmpty())
raise(NonEmptyList.fromListUnsafe(errors))
else
return results
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package arrow.core.continuations

import arrow.core.Either
import arrow.core.NonEmptyList
import arrow.core.identity
import arrow.core.left
import arrow.core.right
Expand All @@ -14,6 +15,7 @@ import io.kotest.property.arbitrary.arbitrary
import io.kotest.property.arbitrary.boolean
import io.kotest.property.arbitrary.flatMap
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.list
import io.kotest.property.arbitrary.long
import io.kotest.property.arbitrary.orNull
import io.kotest.property.arbitrary.string
Expand Down Expand Up @@ -371,6 +373,22 @@ class EffectSpec :
}.message.shouldNotBeNull() shouldBe msg2()
}
}

"accumulate, returns every error" {
checkAll(Arb.list(Arb.int(), range = 2 .. 100)) { errors ->
either<NonEmptyList<Int>, List<String>> {
accumulate(errors) { raise(it) }
} shouldBe NonEmptyList.fromListUnsafe(errors).left()
}
}

"accumulate, returns no error" {
checkAll(Arb.list(Arb.string())) { elements ->
either<NonEmptyList<Int>, List<String>> {
accumulate(elements) { it }
} shouldBe elements.right()
}
}
})

private data class Failure(val msg: String)
Expand Down