From d9673e91a2134a06febca4e301ac838f1285d62c Mon Sep 17 00:00:00 2001 From: "hoc081098@gmail.com" Date: Fri, 18 Apr 2025 16:55:21 +0700 Subject: [PATCH] refactor: simplify zipOrAccumulateNonEmptySet implementation --- .../java/com/hoc/flowmvi/core/EitherNes.kt | 41 ++++--------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/core/src/main/java/com/hoc/flowmvi/core/EitherNes.kt b/core/src/main/java/com/hoc/flowmvi/core/EitherNes.kt index 0f8af29c..e748b8b0 100644 --- a/core/src/main/java/com/hoc/flowmvi/core/EitherNes.kt +++ b/core/src/main/java/com/hoc/flowmvi/core/EitherNes.kt @@ -5,10 +5,6 @@ import arrow.core.NonEmptySet import arrow.core.left import arrow.core.nonEmptySetOf import arrow.core.right -import arrow.core.toNonEmptySetOrNull -import kotlin.contracts.ExperimentalContracts -import kotlin.contracts.InvocationKind -import kotlin.contracts.contract /** * A typealias for [Either] with [NonEmptySet] as the left side. @@ -21,37 +17,16 @@ inline fun A.rightNes(): EitherNes = this.right() @Suppress("NOTHING_TO_INLINE") inline fun E.leftNes(): EitherNes = nonEmptySetOf(this).left() -@OptIn(ExperimentalContracts::class) inline fun Either.Companion.zipOrAccumulateNonEmptySet( a: EitherNes, b: EitherNes, c: EitherNes, transform: (A, B, C) -> Z, -): EitherNes { - contract { callsInPlace(transform, InvocationKind.AT_MOST_ONCE) } - - return if ( - a is Either.Right && - b is Either.Right && - c is Either.Right - ) { - Either.Right( - transform( - a.value, - b.value, - c.value, - ), - ) - } else { - Either.Left( - buildSet(capacity = a.count + b.count + c.count) { - if (a is Either.Left) this.addAll(a.value) - if (b is Either.Left) this.addAll(b.value) - if (c is Either.Left) this.addAll(c.value) - }.toNonEmptySetOrNull()!!, - ) - } -} - -@PublishedApi -internal inline val Either.count: Int get() = if (isRight()) 1 else 0 +): EitherNes = + zipOrAccumulate( + combine = { acc, value -> acc + value }, + a = a, + b = b, + c = c, + transform = transform, + )