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

zipOrAccumulate for Raise #2919

Merged
merged 1 commit into from
Feb 6, 2023
Merged

zipOrAccumulate for Raise #2919

merged 1 commit into from
Feb 6, 2023

Conversation

serras
Copy link
Member

@serras serras commented Feb 6, 2023

Supersedes #2880, targeting main instead of arrow-2.

This PR adds both zipOrAccumulate and mapOrAccumulate to Raise, since the latter was not available in the 1.x branch.

@serras serras requested review from nomisRev and a team February 6, 2023 09:53
@serras serras self-assigned this Feb 6, 2023
@serras serras added the 1.2.0 Tickets belonging to 1.1.2 label Feb 6, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2023

Kover Report

File Coverage [11.46%]
arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/raise/Raise.kt 11.46%
Total Project Coverage 42.62%

Copy link
Collaborator

@franciscodr franciscodr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. @serras

@serras serras merged commit 3eadc65 into main Feb 6, 2023
@serras serras deleted the as-backport-zipOrAccumulate branch February 6, 2023 12:42
Copy link
Member

@nomisRev nomisRev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments, many of the current signatures won't support suspend as they're defined now.

Should we expose these APIs until arity-9 like we do for Either?

*/
@RaiseDSL
public fun <A, B, C> zipOrAccumulate(
semigroup: Semigroup<@UnsafeVariance R>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pending on discussion to remove Semigroup, not blocking for this PR.
Only blocking for 1.2.x release, if we move forward with it's deprecation than we should avoid it in this new signature.

Comment on lines +394 to +397
when (val e = error) {
is EmptyValue -> return results
else -> raise(EmptyValue.unbox(e))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: prefer expressions whenever possible.

Suggested change
when (val e = error) {
is EmptyValue -> return results
else -> raise(EmptyValue.unbox(e))
}
return when (val e = error) {
is EmptyValue -> results
else -> raise(EmptyValue.unbox(e))
}

Comment on lines +384 to +393
val results = mutableListOf<B>()
forEach {
fold<R, B, Unit>({
block(it)
}, { newError ->
error = semigroup.emptyCombine(error, newError)
}, {
results.add(it)
})
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use buildList

Suggested change
val results = mutableListOf<B>()
forEach {
fold<R, B, Unit>({
block(it)
}, { newError ->
error = semigroup.emptyCombine(error, newError)
}, {
results.add(it)
})
}
val results = buildList {
forEach {
fold<R, B, Unit>({
block(it)
}, { newError ->
error = semigroup.emptyCombine(error, newError)
}, {
add(it)
})
}
}

* using the given [semigroup].
*/
@RaiseDSL
public fun <A, B, C, D, E> zipOrAccumulate(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These need to be inline to allow suspend to pass through the lambdas.

Comment on lines +377 to +380
public fun <A, B> Iterable<A>.mapOrAccumulate(
semigroup: Semigroup<@UnsafeVariance R>,
@BuilderInference block: Raise<R>.(A) -> B
): List<B> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be inline to allow suspend to pass through. Cries in context receivers 😭

  public inline fun <A, B> Raise<R>.mapOrAccumulate(
     semigroup: Semigroup<@UnsafeVariance R>,
     list: Iterable<A>,
     @BuilderInference block: Raise<R>.(A) -> B
   ): List<B> {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.2.0 Tickets belonging to 1.1.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants