diff --git a/arrow-libs/core/arrow-core/api/arrow-core.api b/arrow-libs/core/arrow-core/api/arrow-core.api index 7a8703c351f..3ac149f6121 100644 --- a/arrow-libs/core/arrow-core/api/arrow-core.api +++ b/arrow-libs/core/arrow-core/api/arrow-core.api @@ -2943,6 +2943,8 @@ public abstract interface class arrow/typeclasses/Monoid : arrow/typeclasses/Sem public static fun either (Larrow/typeclasses/Monoid;Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid; public abstract fun empty ()Ljava/lang/Object; public static fun endo ()Larrow/typeclasses/Monoid; + public abstract fun fold (Ljava/util/Collection;)Ljava/lang/Object; + public abstract fun fold (Ljava/util/List;)Ljava/lang/Object; public static fun list ()Larrow/typeclasses/Monoid; public static fun map (Larrow/typeclasses/Semigroup;)Larrow/typeclasses/Monoid; public static fun option (Larrow/typeclasses/Semigroup;)Larrow/typeclasses/Monoid; @@ -2973,6 +2975,8 @@ public final class arrow/typeclasses/Monoid$Companion { public final class arrow/typeclasses/Monoid$DefaultImpls { public static fun combineAll (Larrow/typeclasses/Monoid;Ljava/util/Collection;)Ljava/lang/Object; public static fun combineAll (Larrow/typeclasses/Monoid;Ljava/util/List;)Ljava/lang/Object; + public static fun fold (Larrow/typeclasses/Monoid;Ljava/util/Collection;)Ljava/lang/Object; + public static fun fold (Larrow/typeclasses/Monoid;Ljava/util/List;)Ljava/lang/Object; public static fun maybeCombine (Larrow/typeclasses/Monoid;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; public static fun plus (Larrow/typeclasses/Monoid;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt index dac3abec320..e888a363187 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Either.kt @@ -1496,10 +1496,9 @@ public fun Either.combine(SGA: Semigroup, SGB: Semigroup, b: } } +@Deprecated("use fold instead", ReplaceWith("fold(Monoid.either(MA, MB))", "arrow.core.fold", "arrow.typeclasses.Monoid")) public fun Iterable>.combineAll(MA: Monoid, MB: Monoid): Either = - fold(Right(MB.empty()) as Either) { acc, e -> - acc.combine(MA, MB, e) - } + fold(Monoid.either(MA, MB)) /** * Given [B] is a sub type of [C], re-type this value from Either to Either diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt index 0ae4fa187fb..93be4b2909a 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt @@ -657,7 +657,7 @@ public fun Ior.replicate(SA: Semigroup, n: Int): Ior> is Ior.Right -> Ior.Right(List(n) { value }) is Ior.Left -> this is Ior.Both -> bimap( - { List(n - 1) { leftValue }.fold(leftValue, { acc, a -> SA.run { acc + a } }) }, + { List(n - 1) { leftValue }.fold(leftValue) { acc, a -> SA.run { acc + a } } }, { List(n) { rightValue } } ) } @@ -665,11 +665,11 @@ public fun Ior.replicate(SA: Semigroup, n: Int): Ior> public fun Ior.replicate(SA: Semigroup, n: Int, MB: Monoid): Ior = if (n <= 0) Ior.Right(MB.empty()) else when (this) { - is Ior.Right -> Ior.Right(MB.run { List(n) { value }.combineAll() }) + is Ior.Right -> Ior.Right(MB.run { List(n) { value }.fold() }) is Ior.Left -> this is Ior.Both -> bimap( { List(n - 1) { leftValue }.fold(leftValue, { acc, a -> SA.run { acc + a } }) }, - { MB.run { List(n) { rightValue }.combineAll() } } + { MB.run { List(n) { rightValue }.fold() } } ) } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt index 38c07e7320b..1ec37dfbc8b 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt @@ -704,11 +704,9 @@ public fun Iterable>.unalign(): Pair, List> = public inline fun Iterable.unalign(fa: (C) -> Ior): Pair, List> = map(fa).unalign() -public fun Iterable.combineAll(MA: Monoid): A = MA.run { - this@combineAll.fold(empty()) { acc, a -> - acc.combine(a) - } -} +@Deprecated("use fold instead", ReplaceWith("fold(MA)", "arrow.core.fold")) +public fun Iterable.combineAll(MA: Monoid): A = + fold(MA) /** * Returns the first element as [Some(element)][Some], or [None] if the iterable is empty. diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt index fccf9c097a6..9892b527740 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Option.kt @@ -904,14 +904,13 @@ public fun A.some(): Option = Some(this) public fun none(): Option = None +@Deprecated("use fold instead", ReplaceWith("fold(Monoid.option(MA))", "arrow.core.fold", "arrow.typeclasses.Monoid")) public fun Iterable>.combineAll(MA: Monoid): Option = - fold(Option(MA.empty())) { acc, a -> - acc.combine(MA, a) - } + fold(Monoid.option(MA)) -public fun Option.combineAll(MA: Monoid): A = MA.run { - foldLeft(empty()) { acc, a -> acc.combine(a) } -} +@Deprecated("use getOrElse instead", ReplaceWith("getOrElse { MA.empty() }")) +public fun Option.combineAll(MA: Monoid): A = + getOrElse { MA.empty() } public inline fun Option.ensure(error: () -> Unit, predicate: (A) -> Boolean): Option = when (this) { diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Sequence.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Sequence.kt index 806b5f8f999..96d1bd01334 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Sequence.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Sequence.kt @@ -297,11 +297,9 @@ private fun alignRec(ls: Sequence, rs: Sequence): Sequence Sequence.combineAll(MA: Monoid): A = MA.run { - this@combineAll.fold(empty()) { acc, a -> - acc.combine(a) - } -} +@Deprecated("use fold instead", ReplaceWith("fold(MA)", "arrow.core.fold")) +public fun Sequence.combineAll(MA: Monoid): A = + fold(MA) public fun Sequence.crosswalk(f: (A) -> Sequence): Sequence> = fold(emptySequence()) { bs, a -> diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Validated.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Validated.kt index 4c75ca9ba7b..e5eb9824e4c 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Validated.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/Validated.kt @@ -673,6 +673,7 @@ public fun Validated.fold(MA: Monoid): A = MA.run { foldLeft(empty()) { acc, a -> acc.combine(a) } } +@Deprecated("use fold instead", ReplaceWith("fold(MA)", "arrow.core.fold")) public fun Validated.combineAll(MA: Monoid): A = fold(MA) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/map.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/map.kt index 07cb60da96b..f786c2b4f92 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/map.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/map.kt @@ -2,6 +2,7 @@ package arrow.core import arrow.core.Either.Left import arrow.core.Either.Right +import arrow.typeclasses.Monoid import arrow.typeclasses.Semigroup import kotlin.collections.flatMap as _flatMap @@ -122,7 +123,18 @@ public inline fun Map.zip( ): Map { val destination = LinkedHashMap(size) for ((key, bb) in this) { - Nullable.zip(c[key], d[key], e[key], f[key], g[key], h[key]) { cc, dd, ee, ff, gg, hh -> map(key, bb, cc, dd, ee, ff, gg, hh) } + Nullable.zip(c[key], d[key], e[key], f[key], g[key], h[key]) { cc, dd, ee, ff, gg, hh -> + map( + key, + bb, + cc, + dd, + ee, + ff, + gg, + hh + ) + } ?.let { l -> destination.put(key, l) } } return destination @@ -140,7 +152,19 @@ public inline fun Map.zip( ): Map { val destination = LinkedHashMap(size) for ((key, bb) in this) { - Nullable.zip(c[key], d[key], e[key], f[key], g[key], h[key], i[key]) { cc, dd, ee, ff, gg, hh, ii -> map(key, bb, cc, dd, ee, ff, gg, hh, ii) } + Nullable.zip(c[key], d[key], e[key], f[key], g[key], h[key], i[key]) { cc, dd, ee, ff, gg, hh, ii -> + map( + key, + bb, + cc, + dd, + ee, + ff, + gg, + hh, + ii + ) + } ?.let { l -> destination.put(key, l) } } return destination @@ -159,7 +183,16 @@ public inline fun Map.zip( ): Map { val destination = LinkedHashMap(size) for ((key, bb) in this) { - Nullable.zip(c[key], d[key], e[key], f[key], g[key], h[key], i[key], j[key]) { cc, dd, ee, ff, gg, hh, ii, jj -> map(key, bb, cc, dd, ee, ff, gg, hh, ii, jj) } + Nullable.zip( + c[key], + d[key], + e[key], + f[key], + g[key], + h[key], + i[key], + j[key] + ) { cc, dd, ee, ff, gg, hh, ii, jj -> map(key, bb, cc, dd, ee, ff, gg, hh, ii, jj) } ?.let { l -> destination.put(key, l) } } return destination @@ -179,7 +212,17 @@ public inline fun Map.zip( ): Map { val destination = LinkedHashMap(size) for ((key, bb) in this) { - Nullable.zip(c[key], d[key], e[key], f[key], g[key], h[key], i[key], j[key], k[key]) { cc, dd, ee, ff, gg, hh, ii, jj, kk -> + Nullable.zip( + c[key], + d[key], + e[key], + f[key], + g[key], + h[key], + i[key], + j[key], + k[key] + ) { cc, dd, ee, ff, gg, hh, ii, jj, kk -> map(key, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk) }?.let { l -> destination.put(key, l) } } @@ -436,8 +479,9 @@ public fun Map.combine(SG: Semigroup, b: Map): Map = else b.foldLeft(this@combine) { my, (k, a) -> my + Pair(k, a.maybeCombine(my[k])) } } +@Deprecated("use fold instead", ReplaceWith("fold(Monoid.map(SG))", "arrow.core.fold", "arrow.typeclasses.Monoid")) public fun Iterable>.combineAll(SG: Semigroup): Map = - fold(emptyMap()) { acc, map -> acc.combine(SG, map) } + fold(Monoid.map(SG)) public inline fun Map.foldLeft(b: B, f: (B, Map.Entry) -> B): B { var result = b diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Monoid.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Monoid.kt index 9f86760736a..3de4fd8bcfa 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Monoid.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Monoid.kt @@ -7,9 +7,9 @@ import arrow.core.None import arrow.core.Option import arrow.core.Validated import arrow.core.combine -import arrow.core.combineAll import arrow.core.compose import arrow.core.flatten +import arrow.core.fold import arrow.core.identity import kotlin.jvm.JvmName import kotlin.jvm.JvmStatic @@ -24,13 +24,26 @@ public interface Monoid : Semigroup { /** * Combine an [Collection] of [A] values. */ + @Deprecated("use fold instead", ReplaceWith("fold()")) public fun Collection.combineAll(): A = - if (isEmpty()) empty() else reduce { a, b -> a.combine(b) } + fold() /** * Combine an array of [A] values. */ - public fun combineAll(elems: List): A = elems.combineAll() + @Deprecated("use fold instead", ReplaceWith("fold(elems)")) + public fun combineAll(elems: List): A = fold(elems) + + /** + * Fold an [Collection] of [A] values. + */ + public fun Collection.fold(): A = + if (isEmpty()) empty() else reduce { a, b -> a.combine(b) } + + /** + * Fold an array of [A] values. + */ + public fun fold(elems: List): A = elems.fold() public companion object { @JvmStatic @@ -186,14 +199,14 @@ public interface Monoid : Semigroup { override fun Either.combine(b: Either): Either = combine(MOL, MOR, b) - override fun Collection>.combineAll(): Either = - combineAll(MOL, MOR) + override fun Collection>.fold(): Either = + fold(either(MOL, MOR)) + + override fun fold(elems: List>): Either = + elems.fold(either(MOL, MOR)) override fun Either.maybeCombine(b: Either?): Either = b?.let { combine(MOL, MOR, it) } ?: this - - override fun combineAll(elems: List>): Either = - elems.combineAll(MOL, MOR) } private class PairMonoid( diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Fold.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Fold.kt index 3b63a914094..446d625743b 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Fold.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Fold.kt @@ -97,8 +97,9 @@ public interface Fold { /** * Alias for fold. */ + @Deprecated("use fold instead", ReplaceWith("fold(M, source)")) public fun combineAll(M: Monoid, source: S): A = - foldMap(M, source, ::identity) + fold(M, source) /** * Get all targets of the [Fold] diff --git a/arrow-libs/optics/arrow-optics/src/commonTest/kotlin/arrow/optics/EveryTest.kt b/arrow-libs/optics/arrow-optics/src/commonTest/kotlin/arrow/optics/EveryTest.kt index aab5dd0325a..f21eba73a75 100644 --- a/arrow-libs/optics/arrow-optics/src/commonTest/kotlin/arrow/optics/EveryTest.kt +++ b/arrow-libs/optics/arrow-optics/src/commonTest/kotlin/arrow/optics/EveryTest.kt @@ -5,9 +5,7 @@ import arrow.typeclasses.Monoid import io.kotest.matchers.shouldBe import io.kotest.property.Arb import io.kotest.property.arbitrary.int -import io.kotest.property.arbitrary.list import io.kotest.property.arbitrary.orNull -import io.kotest.property.checkAll class EveryTest : UnitSpec() { init {