From da51ac30afc60460bc78fe8858f53ef94890303d Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Mon, 27 Mar 2023 22:37:19 +0200 Subject: [PATCH 1/9] Remove `Monoid` and `Semigroup` combine from replacements --- .../src/commonMain/kotlin/arrow/core/Either.kt | 7 +++---- .../arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt | 8 ++++---- .../src/commonMain/kotlin/arrow/core/Iterable.kt | 8 ++++---- .../src/commonMain/kotlin/arrow/core/NonEmptyList.kt | 2 +- .../src/commonMain/kotlin/arrow/core/Option.kt | 10 +++++----- .../src/commonMain/kotlin/arrow/core/Sequence.kt | 9 ++++----- .../src/commonMain/kotlin/arrow/core/Validated.kt | 8 ++++---- .../arrow-core/src/commonMain/kotlin/arrow/core/map.kt | 5 ++--- .../commonMain/kotlin/arrow/typeclasses/Semigroup.kt | 4 ++-- 9 files changed, 29 insertions(+), 32 deletions(-) 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 3733940fb55..06da8b8ace3 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 @@ -904,7 +904,7 @@ public sealed class Either { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith(" fold({ MN.empty() }, f)") + ReplaceWith(" fold({ empty }, f)") ) public fun foldMap(MN: Monoid, f: (B) -> C): C = fold({ MN.empty() }, f) @@ -2332,8 +2332,7 @@ public fun Either.combine(SGA: Semigroup, SGB: Semigroup, b: @Deprecated( MonoidDeprecation, ReplaceWith( - "fold, Either>(MB.empty().right()) { x, y -> Either.zipOrAccumulate(MA::combine, x, y, MB::combine) }", - "arrow.typeclasses.combine" + "this.fold, Either>(initialValue.right()) { x, y -> Either.zipOrAccumulate({a1, a2 -> a1 + a2}, x, y, {b1, b2 -> b1 + b2}) }" ) ) public fun Iterable>.combineAll(MA: Monoid, MB: Monoid): Either = @@ -2543,7 +2542,7 @@ public inline fun Either.zip( @Deprecated( NicheAPI + "Prefer using the Either DSL, or map", - ReplaceWith("if (n <= 0) Either.Right(MB.empty()) else map { b -> List(n) { b }.fold(MB) }") + ReplaceWith("if (n <= 0) Either.Right(empty) else this.map { b -> List(n) { b }.fold(empty){r, t -> r + t} }") ) public fun Either.replicate(n: Int, MB: Monoid): Either = map { b -> List(n) { b }.fold(MB.empty(), MB::combine) } 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 fc287fddee3..b1c8666fcdf 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 @@ -242,7 +242,7 @@ public sealed class Ior { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith("this.fold({ MN.empty() }, { f }, { _, b -> f(b) })") + ReplaceWith("this.fold({ empty }, { f }, { _, b -> f(b) })") ) public inline fun foldMap(MN: Monoid, f: (B) -> C): C { contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) } @@ -911,7 +911,7 @@ public sealed class Ior { @Deprecated( "$SemigroupDeprecation.", - ReplaceWith("flatMap(SA::combine, f)", "arrow.typeclasses.combine") + ReplaceWith("this.flatMap({a, b -> a + b}, f)") ) public inline fun Ior.flatMap(SG: Semigroup, f: (B) -> Ior): Ior = flatMap(SG::combine, f) @@ -1011,7 +1011,7 @@ public fun Ior, Validated>.bisequenceValidated(S @Deprecated( "$SemigroupDeprecation", - ReplaceWith("combine(other, SA::combine, SB::combine)", "arrow.typeclasses.combine") + ReplaceWith("combine(other, {a1, a2 -> a1 + a2}, {b1, b2 -> b1 + b2} )") ) public fun Ior.combine(SA: Semigroup, SB: Semigroup, other: Ior): Ior = combine(other, SA::combine, SB::combine) @@ -1043,7 +1043,7 @@ public inline fun Ior>.flatten(combine: (A, A) -> A): Ior a1 + a2}") ) public inline fun Ior>.flatten(SA: Semigroup): Ior = flatMap(SA, ::identity) 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 01482e7e798..ddc2af3b260 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 @@ -823,7 +823,7 @@ public fun Iterable.align(b: Iterable): List> = /** * aligns two structures and combine them with the given [Semigroup.combine] */ -@Deprecated(SemigroupDeprecation, ReplaceWith("padZip(other, { it }, { it }, SG::combine)", "arrow.typeclasses.combine")) +@Deprecated(SemigroupDeprecation, ReplaceWith("this.padZip(other, { it }, { it }, {a1, a2 -> a1 + a2})")) public fun Iterable.salign( SG: Semigroup, other: Iterable @@ -928,7 +928,7 @@ public fun Iterable>.unalign(): Pair, List> = separa public inline fun Iterable.unalign(fa: (C) -> Ior): Pair, List> = map(fa).unalign() -@Deprecated("Use fold from Kotlin Std instead", ReplaceWith("fold(MA.empty(), MA::combine)", "arrow.typeclasses.combine")) +@Deprecated("Use fold from Kotlin Std instead", ReplaceWith("fold(initialValue, {a1, a2 -> a1 + a2})")) public fun Iterable.combineAll(MA: Monoid): A = fold(MA.empty(), MA::combine) @@ -1235,11 +1235,11 @@ public fun Iterable.widen(): Iterable = public fun List.widen(): List = this -@Deprecated(MonoidDeprecation, ReplaceWith("fold(MA.empty(), MA::combine)", "arrow.typeclasses.combine")) +@Deprecated(MonoidDeprecation, ReplaceWith("this.fold(initial, {a1, a2 -> a1 + a2})")) public fun Iterable.fold(MA: Monoid): A = fold(MA.empty(), MA::combine) -@Deprecated(MonoidDeprecation, ReplaceWith("fold(MB.empty()) { acc, a -> MB.run { acc.combine(f(a)) } }")) +@Deprecated(MonoidDeprecation, ReplaceWith("this.fold(empty) { acc, a -> combine(acc, f(a)) }")) public fun Iterable.foldMap(MB: Monoid, f: (A) -> B): B = fold(MB.empty()) { acc, a -> MB.run { acc.combine(f(a)) } } diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt index 9a6f8e0ac47..1a763ab77d4 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt @@ -220,7 +220,7 @@ public class NonEmptyList( public fun align(b: NonEmptyList): NonEmptyList> = NonEmptyList(Ior.Both(head, b.head), tail.align(b.tail)) - @Deprecated(SemigroupDeprecation, ReplaceWith("padZip(b, ::identity, ::identity, SA::combine)", "arrow.typeclasses.combine")) + @Deprecated(SemigroupDeprecation, ReplaceWith("padZip(b, ::identity, ::identity, {a1, a2 -> a1 + a2})")) public fun salign(SA: Semigroup<@UnsafeVariance A>, b: NonEmptyList<@UnsafeVariance A>): NonEmptyList = padZip(b, ::identity, ::identity, SA::combine) 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 799d1f21ecb..9221d87969f 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 @@ -1057,7 +1057,7 @@ public sealed class Option { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith("fold({ MB.empty() }, f)") + ReplaceWith("fold({ empty }, f)") ) public inline fun foldMap(MB: Monoid, f: (A) -> B): B = fold({ MB.empty() }, f) @@ -1312,11 +1312,11 @@ public fun A.some(): Option = Some(this) public fun none(): Option = None -@Deprecated(SemigroupDeprecation, ReplaceWith("fold(none()) { x, y -> x.combine(y, MA::combine) }", "arrow.typeclasses.combine")) +@Deprecated(SemigroupDeprecation, ReplaceWith("fold(none()) { x, y -> x.combine(y){a1, a2 -> a1 + a2} }")) public fun Iterable>.combineAll(MA: Monoid): Option = fold(none()) { x, y -> x.combine(y, MA::combine) } -@Deprecated("use getOrElse instead", ReplaceWith("getOrElse { MA.empty() }")) +@Deprecated("use getOrElse instead", ReplaceWith("getOrElse { empty }")) public fun Option.combineAll(MA: Monoid): A = getOrElse { MA.empty() } @@ -1415,7 +1415,7 @@ public inline fun Option.redeemWith(fe: (Unit) -> Option, fb: (A) - @Deprecated( NicheAPI + "Prefer using the Option DSL or map", - ReplaceWith("map { a -> List(n) { a }.fold(MA.empty(), MA::combine) }", "arrow.typeclasses.combine") + ReplaceWith("this.map { a -> List(n) { a }.fold(intialValue){a1, a2 -> a1 + a2} }") ) public fun Option.replicate(n: Int, MA: Monoid): Option = map { a -> List(n) { a }.fold(MA.empty(), MA::combine) } @@ -1633,7 +1633,7 @@ public fun Option.combine(other: Option, combine: (A, A) -> A): Option None -> other } -@Deprecated(SemigroupDeprecation, ReplaceWith("combine(b, SGA::combine)", "arrow.typeclasses.combine")) +@Deprecated(SemigroupDeprecation, ReplaceWith("this.combine(b){a1, a2 -> a1 + a2}")) public fun Option.combine(SGA: Semigroup, b: Option): Option = combine(b, SGA::combine) 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 a831815a9d4..15c10ca465e 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 @@ -426,7 +426,7 @@ public fun Sequence.fold(MA: Monoid): A = MA.run { @Deprecated( "$MonoidDeprecation\n$NicheAPI", - ReplaceWith("fold(initial){ acc, a -> acc, f(a) } }", "arrow.core.sequence") + ReplaceWith("this.fold(initial){ acc, a -> acc + f(a) }") ) public fun Sequence.foldMap(MB: Monoid, f: (A) -> B): B = MB.run { this@foldMap.fold(empty()) { acc, a -> @@ -758,9 +758,8 @@ public fun Sequence>.sequenceOption(): Option> = @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "mapOrAccumulate(semigroup::combine) { it.bind() }.toValidated()", - "arrow.core.mapOrAccumulate", - "arrow.typeclasses.combine" + "this.mapOrAccumulate, Validated, A>({e1, e2 -> e1 + e1}) { it.bind() }.toValidated()", + "arrow.core.mapOrAccumulate" ) ) public fun Sequence>.sequence(semigroup: Semigroup): Validated> = @@ -768,7 +767,7 @@ public fun Sequence>.sequence(semigroup: Semigroup): V @Deprecated( "sequenceValidated is being renamed to sequence to simplify the Arrow API", - ReplaceWith("sequence(semigroup).map { it.asSequence() }", "arrow.core.sequence") + ReplaceWith("this.mapOrAccumulate, Validated, A>({e1, e2 -> e1 + e1}) { it.bind() }.toValidated().map { it.asSequence() }", "arrow.core.mapOrAccumulate") ) public fun Sequence>.sequenceValidated(semigroup: Semigroup): Validated> = sequence(semigroup).map { it.asSequence() } 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 9b4375baafe..710f5166612 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 @@ -306,7 +306,7 @@ public sealed class Validated { @Deprecated( ValidatedDeprMsg + "Use fold on Either after refactoring instead", - ReplaceWith("toEither().fold({ MB.empty() }, f)") + ReplaceWith("toEither().fold({ initial }, f)") ) public inline fun foldMap(MB: Monoid, f: (A) -> B): B = fold({ MB.empty() }, f) @@ -1036,14 +1036,14 @@ public fun Validated.bisequenceNullable(): Validated? = @Deprecated( "$MonoidDeprecation\n$DeprAndNicheMsg\nUse fold on Either after refactoring", - ReplaceWith("fold({ MA.empty() }, ::identity)") + ReplaceWith("fold({ initial }, ::identity)") ) public fun Validated.fold(MA: Monoid): A = fold({ MA.empty() }, ::identity) @Deprecated( "$MonoidDeprecation\n$DeprAndNicheMsg\nUse fold on Either after refactoring", - ReplaceWith("fold({ MA.empty() }, ::identity)", "arrow.core.fold") + ReplaceWith("fold({ initial }, ::identity)", "arrow.core.fold") ) public fun Validated.combineAll(MA: Monoid): A = fold(MA) @@ -1255,7 +1255,7 @@ public inline fun Validated.merge(): A = @Deprecated( ValidatedDeprMsg + "Use Either.zipOrAccumulate instead", - ReplaceWith("Either.zipOrAccumulate(SE::combine, toEither(), y.toEither(), SA::combine).toValidated()") + ReplaceWith("Either.zipOrAccumulate({e1, e2 -> e1 + e2}, this.toEither(), y.toEither(), {a1, a2 -> a1 + a2} ).toValidated()") ) public fun Validated.combine( SE: Semigroup, 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 74849db93bf..f9a85c38c54 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 @@ -315,9 +315,8 @@ public inline fun Map.traverseValidated( @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "mapOrAccumulate(semigroup::combine) { f(it.value).bind() }.toValidated()", - "arrow.core.mapOrAccumulate", - "arrow.typeclasses.combine" + "this.mapOrAccumulate({e1, e2 -> e1 + e2}) { f(it.value).bind() }.toValidated()", + "arrow.core.mapOrAccumulate" ) ) public inline fun Map.traverse( diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Semigroup.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Semigroup.kt index d019b204be3..8748ee32aca 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Semigroup.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/typeclasses/Semigroup.kt @@ -98,7 +98,7 @@ public fun interface Semigroup { @JvmStatic @Deprecated( "$SemigroupDeprecation. Use Either::combine directly instead.", - ReplaceWith("{ a: Either, b: Either -> a.combine(b, SA::combine, SB::combine) }") + ReplaceWith("{ a: Either, b: Either -> a.combine(b, {a1, a2 -> a1 + a2}, {b1, b2 -> b1 + b2}) }") ) public fun either(SA: Semigroup, SB: Semigroup): Semigroup> = EitherSemigroup(SA, SB) @@ -106,7 +106,7 @@ public fun interface Semigroup { @JvmStatic @Deprecated( "$SemigroupDeprecation. Use Ior::combine directly instead.", - ReplaceWith("{ a: Ior, b: Ior -> a.combine(b, SA::combine, SB::combine) }") + ReplaceWith("{ a: Ior, b: Ior -> a.combine(b, {a1, a2 -> a1 + a2}, {b1, b2 -> b1 + b2}) }") ) public fun ior(SA: Semigroup, SB: Semigroup): Semigroup> = IorSemigroup(SA, SB) From 36db03eb777a499b29ad78751dc122422754cc2f Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Mon, 27 Mar 2023 22:38:35 +0200 Subject: [PATCH 2/9] Add missing test --- .../kotlin/arrow/core/EitherTest.kt | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt index 0d88fcfaa83..06616887d60 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt @@ -450,8 +450,42 @@ class EitherTest : StringSpec({ } } } - - "traverse should return list if either is right" { + + "combine should combine 2 eithers" { + checkAll(Arb.either(Arb.string(), Arb.int()), Arb.either(Arb.string(), Arb.int())) { e1, e2 -> + val obtained = e1.combine(e2, { l1, l2 -> l1 + l2 }, { r1, r2 -> r1 + r2 }) + val expected = when(e1){ + is Left -> when(e2) { + is Left -> e1.value + e2.value + is Right -> e1 + } + is Right -> when(e2) { + is Left -> e2 + is Right -> e1.value + e2.value + } + } + obtained shouldBe expected + } + } + + + "combine should combine list of eithers " { + checkAll(Arb.list(Arb.either(Arb.string(), Arb.int()))) { list -> + val obtained = list.fold, Either>(0.right()) { x, y -> + Either.zipOrAccumulate( + { a1, a2 -> a1 + a2 }, + x, + y, + { b1, b2 -> b1 + b2 }) + } + val expected = list[0] + obtained shouldBe expected + } + } + + + + "traverse should return list if either is right" { val right: Either = Right(1) val left: Either = Left("foo") From 659c0807a9dc267775957e04fda6dfbe86754e3c Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Tue, 28 Mar 2023 17:03:00 +0200 Subject: [PATCH 3/9] Deprecate `foldMap` with `Monoid` in `Fold` --- .../optics/arrow-optics/api/arrow-optics.api | 9 +- .../commonMain/kotlin/arrow/optics/Every.kt | 94 +++++++++++++++++++ .../commonMain/kotlin/arrow/optics/Fold.kt | 44 +++++++-- .../commonMain/kotlin/arrow/optics/Getter.kt | 5 + .../src/commonMain/kotlin/arrow/optics/Iso.kt | 5 + .../commonMain/kotlin/arrow/optics/Lens.kt | 5 + .../kotlin/arrow/optics/Optional.kt | 6 ++ .../kotlin/arrow/optics/OptionalGetter.kt | 5 + .../commonMain/kotlin/arrow/optics/Prism.kt | 5 + .../arrow/optics/typeclasses/FilterIndex.kt | 12 +++ 10 files changed, 181 insertions(+), 9 deletions(-) diff --git a/arrow-libs/optics/arrow-optics/api/arrow-optics.api b/arrow-libs/optics/arrow-optics/api/arrow-optics.api index 9a570cb45a7..e9bbd5727aa 100644 --- a/arrow-libs/optics/arrow-optics/api/arrow-optics.api +++ b/arrow-libs/optics/arrow-optics/api/arrow-optics.api @@ -88,7 +88,6 @@ public final class arrow/optics/Fold$DefaultImpls { public static fun firstOrNull (Larrow/optics/Fold;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/Fold;Larrow/typeclasses/Monoid;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/Fold;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;)Ljava/lang/Object; - public static fun foldMap (Larrow/optics/Fold;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun getAll (Larrow/optics/Fold;Ljava/lang/Object;)Ljava/util/List; public static fun isEmpty (Larrow/optics/Fold;Ljava/lang/Object;)Z public static fun isNotEmpty (Larrow/optics/Fold;Ljava/lang/Object;)Z @@ -105,6 +104,7 @@ public abstract interface class arrow/optics/Getter : arrow/optics/Fold { public abstract fun compose (Larrow/optics/Getter;)Larrow/optics/Getter; public abstract fun first ()Larrow/optics/Getter; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun get (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun left ()Larrow/optics/Getter; public abstract fun plus (Larrow/optics/Getter;)Larrow/optics/Getter; @@ -173,6 +173,7 @@ public abstract interface class arrow/optics/PEvery : arrow/optics/Fold, arrow/o public abstract fun compose (Larrow/optics/PEvery;)Larrow/optics/PEvery; public static fun either ()Larrow/optics/PEvery; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getEvery (Larrow/optics/PIso;)Larrow/optics/PEvery; public abstract fun getEvery (Larrow/optics/PLens;)Larrow/optics/PEvery; public abstract fun getEvery (Larrow/optics/POptional;)Larrow/optics/PEvery; @@ -234,7 +235,6 @@ public final class arrow/optics/PEvery$DefaultImpls { public static fun firstOrNull (Larrow/optics/PEvery;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/PEvery;Larrow/typeclasses/Monoid;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/PEvery;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;)Ljava/lang/Object; - public static fun foldMap (Larrow/optics/PEvery;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun getAll (Larrow/optics/PEvery;Ljava/lang/Object;)Ljava/util/List; public static fun getEvery (Larrow/optics/PEvery;Larrow/optics/PEvery;)Larrow/optics/PTraversal; public static fun getEvery (Larrow/optics/PEvery;Larrow/optics/PIso;)Larrow/optics/PEvery; @@ -264,6 +264,7 @@ public abstract interface class arrow/optics/PIso : arrow/optics/Fold, arrow/opt public static fun eitherToValidated ()Larrow/optics/PIso; public abstract fun first ()Larrow/optics/PIso; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun get (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public abstract fun left ()Larrow/optics/PIso; @@ -385,6 +386,7 @@ public abstract interface class arrow/optics/PLens : arrow/optics/Getter, arrow/ public abstract fun compose (Larrow/optics/PLens;)Larrow/optics/PLens; public abstract fun first ()Larrow/optics/PLens; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun get (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public static fun nonEmptyListHead ()Larrow/optics/PLens; @@ -490,6 +492,7 @@ public abstract interface class arrow/optics/POptional : arrow/optics/PEvery, ar public abstract fun compose (Larrow/optics/POptional;)Larrow/optics/POptional; public abstract fun first ()Larrow/optics/POptional; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public static fun listHead ()Larrow/optics/POptional; public static fun listTail ()Larrow/optics/POptional; @@ -570,6 +573,7 @@ public abstract interface class arrow/optics/POptionalGetter : arrow/optics/Fold public static fun filter (Lkotlin/jvm/functions/Function1;)Larrow/optics/POptionalGetter; public abstract fun first ()Larrow/optics/POptionalGetter; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public abstract fun getOrNull (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun plus (Larrow/optics/POptionalGetter;)Larrow/optics/POptionalGetter; @@ -618,6 +622,7 @@ public abstract interface class arrow/optics/PPrism : arrow/optics/PEvery, arrow public static fun eitherRight ()Larrow/optics/PPrism; public abstract fun first ()Larrow/optics/PPrism; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public abstract fun left ()Larrow/optics/PPrism; public abstract fun liftNullable (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function1; diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt index dc8fdad411f..c100b75b89d 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt @@ -28,6 +28,8 @@ public interface PEvery : PTraversal, Fold, PSette */ override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R + override fun modify(source: S, map: (focus: A) -> B): T /** @@ -40,6 +42,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun modify(source: S, map: (focus: C) -> D): T = this@PEvery.modify(source) { b -> other.modify(b, map) } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: C) -> R): R = + this@PEvery.foldMap(empty, combine, source) { c -> other.foldMap(empty, combine, c, map) } } public operator fun plus(other: PEvery): PEvery = @@ -50,6 +55,8 @@ public interface PEvery : PTraversal, Fold, PSette object : Every { override fun foldMap(M: Monoid, source: S, map: (A) -> R): R = F.foldMap(M, source, map) override fun modify(source: S, map: (focus: A) -> A): S = T.modify(source, map) + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + F.foldMap(empty, combine, source, map) } /** @@ -63,6 +70,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: List, map: (focus: A) -> R): R = source.foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: List, map: (focus: A) -> R): R = + source.fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -78,6 +88,10 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Either, map: (focus: R) -> A): A = source.fold({ M.empty() }, map) + + override fun foldMap(empty: A, combine: (A, A) -> A, source: Either, map: (focus: R) -> A): A = + source.fold({ empty }, map) + } @JvmStatic @@ -89,6 +103,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Map, map: (focus: V) -> R): R = M.run { source.fold(empty()) { acc, (_, v) -> acc.combine(map(v)) } } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Map, map: (focus: V) -> R): R = + source.fold(empty) { acc, (_, v) -> combine(acc, map(v)) } } /** @@ -105,6 +122,10 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: NonEmptyList, map: (focus: A) -> R): R = source.foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: NonEmptyList, map: (focus: A) -> R): R = + source.fold(empty) { acc, a -> combine(acc, map(a)) } + } /** @@ -121,6 +142,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Option, map: (focus: A) -> R): R = source.fold({ M.empty() }, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Option, map: (focus: A) -> R): R = + source.fold({ empty }, map) } @JvmStatic @@ -131,6 +155,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Sequence, map: (focus: A) -> R): R = source.foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Sequence, map: (focus: A) -> R): R = + source.fold(empty){ acc, a -> combine(acc, map(a)) } } /** @@ -148,6 +175,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: String, map: (focus: Char) -> R): R = M.run { source.fold(empty()) { acc, char -> acc.combine(map(char)) } } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: String, map: (focus: Char) -> R): R = + source.fold(empty) { acc, char -> combine(acc, map(char)) } } /** @@ -162,6 +192,10 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Pair, map: (focus: A) -> R): R = listOf(source.first, source.second) .foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Pair, map: (focus: A) -> R): R = + listOf(source.first, source.second).fold(empty) { acc, a -> combine(acc, map(a)) } + } /** @@ -176,6 +210,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Triple, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third) .foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Triple, map: (focus: A) -> R): R = + listOf(source.first, source.second, source.third).fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -190,6 +227,9 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple4, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth) .foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Tuple4, map: (focus: A) -> R): R = + listOf(source.first, source.second, source.third, source.fourth).fold(empty) { acc, a -> combine(acc, map(a))} } /** @@ -204,6 +244,15 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple5, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth) .foldMap(M, map) + + override fun foldMap( + empty: R, + combine: (R, R) -> R, + source: Tuple5, + map: (focus: A) -> R + ): R = + listOf(source.first, source.second, source.third, source.fourth, source.fifth) + .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -225,6 +274,15 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple6, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth) .foldMap(M, map) + + override fun foldMap( + empty: R, + combine: (R, R) -> R, + source: Tuple6, + map: (focus: A) -> R + ): R = + listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth) + .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -247,6 +305,15 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple7, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh) .foldMap(M, map) + + override fun foldMap( + empty: R, + combine: (R, R) -> R, + source: Tuple7, + map: (focus: A) -> R + ): R = + listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh) + .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -273,6 +340,15 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple8, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth) .foldMap(M, map) + + override fun foldMap( + empty: R, + combine: (R, R) -> R, + source: Tuple8, + map: (focus: A) -> R + ): R = + listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth) + .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -300,6 +376,15 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple9, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth) .foldMap(M, map) + + override fun foldMap( + empty: R, + combine: (R, R) -> R, + source: Tuple9, + map: (focus: A) -> R + ): R = + listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth) + .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -328,6 +413,15 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple10, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth, source.tenth) .foldMap(M, map) + + override fun foldMap( + empty: R, + combine: (R, R) -> R, + source: Tuple10, + map: (focus: A) -> R + ): R = + listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth, source.tenth) + .fold(empty) { acc, a -> combine(acc, map(a)) } } } 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 879be12d950..a05bad6519c 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 @@ -29,16 +29,12 @@ public interface Fold { /** * Map each target to a type [R] and combine the results as a fold. */ - public fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - foldMap(object : Monoid { - override fun empty(): R = empty - override fun R.combine(b: R): R = combine(this, b) - }, source, map) + public fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R /** * Map each target to a type R and use a Monoid to fold the results */ - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(M.empty(), M::combine, source, map)", "arrow.optics.foldMap", "arrow.typeclasses.combine")) + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) public fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R /** @@ -92,7 +88,7 @@ public interface Fold { /** * Fold using the given [Monoid] instance. */ - @Deprecated(MonoidDeprecation, ReplaceWith("fold(M.empty(), M::combine, source)", "arrow.optics.fold", "arrow.typeclasses.combine")) + @Deprecated(MonoidDeprecation, ReplaceWith("fold(empty, {r1, r2 -> r1 + r2}, source)", "arrow.optics.fold")) public fun fold(M: Monoid, source: S): A = foldMap(M, source, ::identity) @@ -132,8 +128,13 @@ public interface Fold { */ public infix fun choice(other: Fold): Fold, A> = object : Fold, A> { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (focus: A) -> R): R = source.fold({ ac -> this@Fold.foldMap(M, ac, map) }, { c -> other.foldMap(M, c, map) }) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Either, map: (focus: A) -> R): R = + source.fold({ ac -> this@Fold.foldMap(empty, combine, ac, map) }, { c -> other.foldMap(empty, combine, c, map) }) + } /** @@ -141,10 +142,17 @@ public interface Fold { */ public fun left(): Fold, Either> = object : Fold, Either> { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (Either) -> R): R = source.fold( { a1: S -> this@Fold.foldMap(M, a1) { b -> map(Either.Left(b)) } }, { c -> map(Either.Right(c)) }) + + override fun foldMap(empty: R, combine:(R, R) -> R, source: Either, map: (focus: Either) -> R): R = + source.fold( + { a1: S -> this@Fold.foldMap(empty, combine, a1) { b -> map(Either.Left(b)) } }, + { c -> map(Either.Right(c)) } + ) } /** @@ -152,8 +160,12 @@ public interface Fold { */ public fun right(): Fold, Either> = object : Fold, Either> { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (Either) -> R): R = source.fold({ c -> map(Either.Left(c)) }, { a1 -> this@Fold.foldMap(M, a1) { b -> map(Either.Right(b)) } }) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Either, map: (focus: Either) -> R): R = + source.fold({ c -> map(Either.Left(c)) }, { a1 -> this@Fold.foldMap(empty, combine, a1) { b -> map(Either.Right(b)) } }) } /** @@ -161,8 +173,13 @@ public interface Fold { */ public infix fun compose(other: Fold): Fold = object : Fold { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: C) -> R): R = this@Fold.foldMap(M, source) { c -> other.foldMap(M, c, map) } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: C) -> R): R = + this@Fold.foldMap(empty, combine, source) { c -> other.foldMap(empty, combine, c, map) } + } public operator fun plus(other: Fold): Fold = @@ -177,16 +194,25 @@ public interface Fold { * [Fold] that takes either [S] or [S] and strips the choice of [S]. */ public fun codiagonal(): Fold, S> = object : Fold, S> { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (S) -> R): R = source.fold(map, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Either, map: (focus: S) -> R): R = + source.fold(map, map) } /** * Creates a [Fold] based on a predicate of the source [S] */ public fun select(p: (S) -> Boolean): Fold = object : Fold { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (S) -> R): R = if (p(source)) map(source) else M.empty() + + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: S) -> R): R = + if (p(source)) map(source) else empty + } /** @@ -198,8 +224,12 @@ public interface Fold { @JvmStatic public fun iterable(): Fold, A> = object : Fold, A> { + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Iterable, map: (focus: A) -> R): R = source.foldMap(M, map) + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Iterable, map: (focus: A) -> R): R = + source.fold(empty) { acc, a -> combine(acc, map(a)) } } /** diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt index 7fa4469e60d..5e641453033 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt @@ -4,6 +4,7 @@ import arrow.core.Either import arrow.core.compose import arrow.core.identity import arrow.typeclasses.Monoid +import arrow.typeclasses.MonoidDeprecation /** * A [Getter] is an optic that allows to see into a structure and getting a focus. @@ -21,9 +22,13 @@ public fun interface Getter : Fold { */ public fun get(source: S): A + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = map(get(source)) + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + map(get(source)) + /** * Create a product of the [Getter] and a type [C] */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt index 5f941198744..92150a95ef2 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt @@ -12,6 +12,7 @@ import arrow.core.Validated.Valid import arrow.core.compose import arrow.core.identity import arrow.typeclasses.Monoid +import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -65,9 +66,13 @@ public interface PIso : PPrism, PLens, Gette override fun modify(source: S, map: (focus: A) -> B): T = reverseGet(map(get(source))) + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = map(get(source)) + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + map(get(source)) + /** * Reverse a [PIso]: the source becomes the target and the target becomes the source */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt index 6912a35d6bd..b57c701f759 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt @@ -4,6 +4,7 @@ import arrow.core.Either import arrow.core.NonEmptyList import arrow.core.identity import arrow.typeclasses.Monoid +import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -38,9 +39,13 @@ public interface PLens : Getter, POptional, PSette override fun getOrModify(source: S): Either = Either.Right(get(source)) + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = map(get(source)) + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + map(get(source)) + /** * Join two [PLens] with the same focus in [A] */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt index 5ccbb8d4b54..50fc654048c 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt @@ -10,6 +10,7 @@ import arrow.core.identity import arrow.core.prependTo import arrow.core.toOption import arrow.typeclasses.Monoid +import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -72,9 +73,14 @@ public interface POptional : PSetter, POptionalGetter + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = getOrModify(source).map(map).getOrElse { M.empty() } + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + getOrModify(source).map(map).getOrElse { empty } + + /** * Modify the focus of a [POptional] with a function [map] */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt index efa0b790678..b05adb11b93 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt @@ -7,6 +7,7 @@ import arrow.core.Some import arrow.core.flatMap import arrow.core.getOrElse import arrow.typeclasses.Monoid +import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -37,9 +38,13 @@ public interface POptionalGetter: Fold { public fun getOrNull(source: S): A? = getOrModify(source).getOrNull() + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = getOrModify(source).map(map).getOrElse { M.empty() } + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + getOrModify(source).map(map).getOrElse { empty } + /** * Join two [POptionalGetter] with the same focus */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt index 96e420e9415..b4be2ccdf2a 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt @@ -10,6 +10,7 @@ import arrow.core.identity import arrow.core.left import arrow.core.right import arrow.typeclasses.Monoid +import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmName import kotlin.jvm.JvmStatic @@ -41,9 +42,13 @@ public interface PPrism : POptional, PSetter public fun reverseGet(focus: B): T + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = getOrNull(source)?.let(map) ?: M.empty() + override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + getOrNull(source)?.let(map) ?: empty + /** * Modify the focus of a [PPrism] with a function */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt index 16c0d920bc6..042ee01fbc8 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt @@ -43,6 +43,9 @@ public fun interface FilterIndex { override fun modify(source: List, map: (focus: A) -> A): List = source.mapIndexed { index, a -> if (p(index)) map(a) else a } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: List, map: (focus: A) -> R): R = + source.foldIndexed(empty) { index, acc, a -> if (p(index)) combine(acc, map(a)) else acc } } } @@ -58,6 +61,9 @@ public fun interface FilterIndex { override fun modify(source: Map, map: (focus: V) -> V): Map = source.mapValues { (k, v) -> if (p(k)) map(v) else v } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Map, map: (focus: V) -> R): R = + source.entries.fold(empty) { acc, (k, v) -> if (p(k)) combine(acc, map(v)) else acc } } } @@ -77,6 +83,9 @@ public fun interface FilterIndex { override fun modify(source: NonEmptyList, map: (focus: A) -> A): NonEmptyList = source.mapIndexed { index, a -> if (p(index)) map(a) else a }.toNonEmptyListOrNull() ?: throw IndexOutOfBoundsException("Empty list doesn't contain element at index 0.") + + override fun foldMap(empty: R, combine: (R, R) -> R, source: NonEmptyList, map: (focus: A) -> R): R = + source.foldIndexed(empty) { index, acc, r -> if (p(index)) combine(acc, map(r)) else acc } } } @@ -92,6 +101,9 @@ public fun interface FilterIndex { override fun modify(source: Sequence, map: (focus: A) -> A): Sequence = source.mapIndexed { index, a -> if (p(index)) map(a) else a } + + override fun foldMap(empty: R, combine: (R, R) -> R, source: Sequence, map: (focus: A) -> R): R = + source.foldIndexed(empty) { index, acc, a -> if (p(index)) combine(acc, map(a)) else acc } } } From eed2d83bec42c5654a96089f098179aef1f78bb7 Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Tue, 28 Mar 2023 22:25:30 +0200 Subject: [PATCH 4/9] Revert "Deprecate `foldMap` with `Monoid` in `Fold`" This reverts commit 659c0807a9dc267775957e04fda6dfbe86754e3c. --- .../optics/arrow-optics/api/arrow-optics.api | 9 +- .../commonMain/kotlin/arrow/optics/Every.kt | 94 ------------------- .../commonMain/kotlin/arrow/optics/Fold.kt | 44 ++------- .../commonMain/kotlin/arrow/optics/Getter.kt | 5 - .../src/commonMain/kotlin/arrow/optics/Iso.kt | 5 - .../commonMain/kotlin/arrow/optics/Lens.kt | 5 - .../kotlin/arrow/optics/Optional.kt | 6 -- .../kotlin/arrow/optics/OptionalGetter.kt | 5 - .../commonMain/kotlin/arrow/optics/Prism.kt | 5 - .../arrow/optics/typeclasses/FilterIndex.kt | 12 --- 10 files changed, 9 insertions(+), 181 deletions(-) diff --git a/arrow-libs/optics/arrow-optics/api/arrow-optics.api b/arrow-libs/optics/arrow-optics/api/arrow-optics.api index e9bbd5727aa..9a570cb45a7 100644 --- a/arrow-libs/optics/arrow-optics/api/arrow-optics.api +++ b/arrow-libs/optics/arrow-optics/api/arrow-optics.api @@ -88,6 +88,7 @@ public final class arrow/optics/Fold$DefaultImpls { public static fun firstOrNull (Larrow/optics/Fold;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/Fold;Larrow/typeclasses/Monoid;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/Fold;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;)Ljava/lang/Object; + public static fun foldMap (Larrow/optics/Fold;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun getAll (Larrow/optics/Fold;Ljava/lang/Object;)Ljava/util/List; public static fun isEmpty (Larrow/optics/Fold;Ljava/lang/Object;)Z public static fun isNotEmpty (Larrow/optics/Fold;Ljava/lang/Object;)Z @@ -104,7 +105,6 @@ public abstract interface class arrow/optics/Getter : arrow/optics/Fold { public abstract fun compose (Larrow/optics/Getter;)Larrow/optics/Getter; public abstract fun first ()Larrow/optics/Getter; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun get (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun left ()Larrow/optics/Getter; public abstract fun plus (Larrow/optics/Getter;)Larrow/optics/Getter; @@ -173,7 +173,6 @@ public abstract interface class arrow/optics/PEvery : arrow/optics/Fold, arrow/o public abstract fun compose (Larrow/optics/PEvery;)Larrow/optics/PEvery; public static fun either ()Larrow/optics/PEvery; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getEvery (Larrow/optics/PIso;)Larrow/optics/PEvery; public abstract fun getEvery (Larrow/optics/PLens;)Larrow/optics/PEvery; public abstract fun getEvery (Larrow/optics/POptional;)Larrow/optics/PEvery; @@ -235,6 +234,7 @@ public final class arrow/optics/PEvery$DefaultImpls { public static fun firstOrNull (Larrow/optics/PEvery;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/PEvery;Larrow/typeclasses/Monoid;Ljava/lang/Object;)Ljava/lang/Object; public static fun fold (Larrow/optics/PEvery;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;)Ljava/lang/Object; + public static fun foldMap (Larrow/optics/PEvery;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static fun getAll (Larrow/optics/PEvery;Ljava/lang/Object;)Ljava/util/List; public static fun getEvery (Larrow/optics/PEvery;Larrow/optics/PEvery;)Larrow/optics/PTraversal; public static fun getEvery (Larrow/optics/PEvery;Larrow/optics/PIso;)Larrow/optics/PEvery; @@ -264,7 +264,6 @@ public abstract interface class arrow/optics/PIso : arrow/optics/Fold, arrow/opt public static fun eitherToValidated ()Larrow/optics/PIso; public abstract fun first ()Larrow/optics/PIso; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun get (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public abstract fun left ()Larrow/optics/PIso; @@ -386,7 +385,6 @@ public abstract interface class arrow/optics/PLens : arrow/optics/Getter, arrow/ public abstract fun compose (Larrow/optics/PLens;)Larrow/optics/PLens; public abstract fun first ()Larrow/optics/PLens; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun get (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public static fun nonEmptyListHead ()Larrow/optics/PLens; @@ -492,7 +490,6 @@ public abstract interface class arrow/optics/POptional : arrow/optics/PEvery, ar public abstract fun compose (Larrow/optics/POptional;)Larrow/optics/POptional; public abstract fun first ()Larrow/optics/POptional; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public static fun listHead ()Larrow/optics/POptional; public static fun listTail ()Larrow/optics/POptional; @@ -573,7 +570,6 @@ public abstract interface class arrow/optics/POptionalGetter : arrow/optics/Fold public static fun filter (Lkotlin/jvm/functions/Function1;)Larrow/optics/POptionalGetter; public abstract fun first ()Larrow/optics/POptionalGetter; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public abstract fun getOrNull (Ljava/lang/Object;)Ljava/lang/Object; public abstract fun plus (Larrow/optics/POptionalGetter;)Larrow/optics/POptionalGetter; @@ -622,7 +618,6 @@ public abstract interface class arrow/optics/PPrism : arrow/optics/PEvery, arrow public static fun eitherRight ()Larrow/optics/PPrism; public abstract fun first ()Larrow/optics/PPrism; public abstract fun foldMap (Larrow/typeclasses/Monoid;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public abstract fun foldMap (Ljava/lang/Object;Lkotlin/jvm/functions/Function2;Ljava/lang/Object;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; public abstract fun getOrModify (Ljava/lang/Object;)Larrow/core/Either; public abstract fun left ()Larrow/optics/PPrism; public abstract fun liftNullable (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function1; diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt index c100b75b89d..dc8fdad411f 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Every.kt @@ -28,8 +28,6 @@ public interface PEvery : PTraversal, Fold, PSette */ override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R - override fun modify(source: S, map: (focus: A) -> B): T /** @@ -42,9 +40,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun modify(source: S, map: (focus: C) -> D): T = this@PEvery.modify(source) { b -> other.modify(b, map) } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: C) -> R): R = - this@PEvery.foldMap(empty, combine, source) { c -> other.foldMap(empty, combine, c, map) } } public operator fun plus(other: PEvery): PEvery = @@ -55,8 +50,6 @@ public interface PEvery : PTraversal, Fold, PSette object : Every { override fun foldMap(M: Monoid, source: S, map: (A) -> R): R = F.foldMap(M, source, map) override fun modify(source: S, map: (focus: A) -> A): S = T.modify(source, map) - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - F.foldMap(empty, combine, source, map) } /** @@ -70,9 +63,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: List, map: (focus: A) -> R): R = source.foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: List, map: (focus: A) -> R): R = - source.fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -88,10 +78,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Either, map: (focus: R) -> A): A = source.fold({ M.empty() }, map) - - override fun foldMap(empty: A, combine: (A, A) -> A, source: Either, map: (focus: R) -> A): A = - source.fold({ empty }, map) - } @JvmStatic @@ -103,9 +89,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Map, map: (focus: V) -> R): R = M.run { source.fold(empty()) { acc, (_, v) -> acc.combine(map(v)) } } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Map, map: (focus: V) -> R): R = - source.fold(empty) { acc, (_, v) -> combine(acc, map(v)) } } /** @@ -122,10 +105,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: NonEmptyList, map: (focus: A) -> R): R = source.foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: NonEmptyList, map: (focus: A) -> R): R = - source.fold(empty) { acc, a -> combine(acc, map(a)) } - } /** @@ -142,9 +121,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Option, map: (focus: A) -> R): R = source.fold({ M.empty() }, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Option, map: (focus: A) -> R): R = - source.fold({ empty }, map) } @JvmStatic @@ -155,9 +131,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Sequence, map: (focus: A) -> R): R = source.foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Sequence, map: (focus: A) -> R): R = - source.fold(empty){ acc, a -> combine(acc, map(a)) } } /** @@ -175,9 +148,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: String, map: (focus: Char) -> R): R = M.run { source.fold(empty()) { acc, char -> acc.combine(map(char)) } } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: String, map: (focus: Char) -> R): R = - source.fold(empty) { acc, char -> combine(acc, map(char)) } } /** @@ -192,10 +162,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Pair, map: (focus: A) -> R): R = listOf(source.first, source.second) .foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Pair, map: (focus: A) -> R): R = - listOf(source.first, source.second).fold(empty) { acc, a -> combine(acc, map(a)) } - } /** @@ -210,9 +176,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Triple, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third) .foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Triple, map: (focus: A) -> R): R = - listOf(source.first, source.second, source.third).fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -227,9 +190,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple4, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth) .foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Tuple4, map: (focus: A) -> R): R = - listOf(source.first, source.second, source.third, source.fourth).fold(empty) { acc, a -> combine(acc, map(a))} } /** @@ -244,15 +204,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple5, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth) .foldMap(M, map) - - override fun foldMap( - empty: R, - combine: (R, R) -> R, - source: Tuple5, - map: (focus: A) -> R - ): R = - listOf(source.first, source.second, source.third, source.fourth, source.fifth) - .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -274,15 +225,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple6, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth) .foldMap(M, map) - - override fun foldMap( - empty: R, - combine: (R, R) -> R, - source: Tuple6, - map: (focus: A) -> R - ): R = - listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth) - .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -305,15 +247,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple7, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh) .foldMap(M, map) - - override fun foldMap( - empty: R, - combine: (R, R) -> R, - source: Tuple7, - map: (focus: A) -> R - ): R = - listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh) - .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -340,15 +273,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple8, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth) .foldMap(M, map) - - override fun foldMap( - empty: R, - combine: (R, R) -> R, - source: Tuple8, - map: (focus: A) -> R - ): R = - listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth) - .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -376,15 +300,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple9, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth) .foldMap(M, map) - - override fun foldMap( - empty: R, - combine: (R, R) -> R, - source: Tuple9, - map: (focus: A) -> R - ): R = - listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth) - .fold(empty) { acc, a -> combine(acc, map(a)) } } /** @@ -413,15 +328,6 @@ public interface PEvery : PTraversal, Fold, PSette override fun foldMap(M: Monoid, source: Tuple10, map: (focus: A) -> R): R = listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth, source.tenth) .foldMap(M, map) - - override fun foldMap( - empty: R, - combine: (R, R) -> R, - source: Tuple10, - map: (focus: A) -> R - ): R = - listOf(source.first, source.second, source.third, source.fourth, source.fifth, source.sixth, source.seventh, source.eighth, source.ninth, source.tenth) - .fold(empty) { acc, a -> combine(acc, map(a)) } } } 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 a05bad6519c..879be12d950 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 @@ -29,12 +29,16 @@ public interface Fold { /** * Map each target to a type [R] and combine the results as a fold. */ - public fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R + public fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = + foldMap(object : Monoid { + override fun empty(): R = empty + override fun R.combine(b: R): R = combine(this, b) + }, source, map) /** * Map each target to a type R and use a Monoid to fold the results */ - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) + @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(M.empty(), M::combine, source, map)", "arrow.optics.foldMap", "arrow.typeclasses.combine")) public fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R /** @@ -88,7 +92,7 @@ public interface Fold { /** * Fold using the given [Monoid] instance. */ - @Deprecated(MonoidDeprecation, ReplaceWith("fold(empty, {r1, r2 -> r1 + r2}, source)", "arrow.optics.fold")) + @Deprecated(MonoidDeprecation, ReplaceWith("fold(M.empty(), M::combine, source)", "arrow.optics.fold", "arrow.typeclasses.combine")) public fun fold(M: Monoid, source: S): A = foldMap(M, source, ::identity) @@ -128,13 +132,8 @@ public interface Fold { */ public infix fun choice(other: Fold): Fold, A> = object : Fold, A> { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (focus: A) -> R): R = source.fold({ ac -> this@Fold.foldMap(M, ac, map) }, { c -> other.foldMap(M, c, map) }) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Either, map: (focus: A) -> R): R = - source.fold({ ac -> this@Fold.foldMap(empty, combine, ac, map) }, { c -> other.foldMap(empty, combine, c, map) }) - } /** @@ -142,17 +141,10 @@ public interface Fold { */ public fun left(): Fold, Either> = object : Fold, Either> { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (Either) -> R): R = source.fold( { a1: S -> this@Fold.foldMap(M, a1) { b -> map(Either.Left(b)) } }, { c -> map(Either.Right(c)) }) - - override fun foldMap(empty: R, combine:(R, R) -> R, source: Either, map: (focus: Either) -> R): R = - source.fold( - { a1: S -> this@Fold.foldMap(empty, combine, a1) { b -> map(Either.Left(b)) } }, - { c -> map(Either.Right(c)) } - ) } /** @@ -160,12 +152,8 @@ public interface Fold { */ public fun right(): Fold, Either> = object : Fold, Either> { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (Either) -> R): R = source.fold({ c -> map(Either.Left(c)) }, { a1 -> this@Fold.foldMap(M, a1) { b -> map(Either.Right(b)) } }) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Either, map: (focus: Either) -> R): R = - source.fold({ c -> map(Either.Left(c)) }, { a1 -> this@Fold.foldMap(empty, combine, a1) { b -> map(Either.Right(b)) } }) } /** @@ -173,13 +161,8 @@ public interface Fold { */ public infix fun compose(other: Fold): Fold = object : Fold { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: C) -> R): R = this@Fold.foldMap(M, source) { c -> other.foldMap(M, c, map) } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: C) -> R): R = - this@Fold.foldMap(empty, combine, source) { c -> other.foldMap(empty, combine, c, map) } - } public operator fun plus(other: Fold): Fold = @@ -194,25 +177,16 @@ public interface Fold { * [Fold] that takes either [S] or [S] and strips the choice of [S]. */ public fun codiagonal(): Fold, S> = object : Fold, S> { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Either, map: (S) -> R): R = source.fold(map, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Either, map: (focus: S) -> R): R = - source.fold(map, map) } /** * Creates a [Fold] based on a predicate of the source [S] */ public fun select(p: (S) -> Boolean): Fold = object : Fold { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (S) -> R): R = if (p(source)) map(source) else M.empty() - - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: S) -> R): R = - if (p(source)) map(source) else empty - } /** @@ -224,12 +198,8 @@ public interface Fold { @JvmStatic public fun iterable(): Fold, A> = object : Fold, A> { - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: Iterable, map: (focus: A) -> R): R = source.foldMap(M, map) - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Iterable, map: (focus: A) -> R): R = - source.fold(empty) { acc, a -> combine(acc, map(a)) } } /** diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt index 5e641453033..7fa4469e60d 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Getter.kt @@ -4,7 +4,6 @@ import arrow.core.Either import arrow.core.compose import arrow.core.identity import arrow.typeclasses.Monoid -import arrow.typeclasses.MonoidDeprecation /** * A [Getter] is an optic that allows to see into a structure and getting a focus. @@ -22,13 +21,9 @@ public fun interface Getter : Fold { */ public fun get(source: S): A - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = map(get(source)) - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - map(get(source)) - /** * Create a product of the [Getter] and a type [C] */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt index 92150a95ef2..5f941198744 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Iso.kt @@ -12,7 +12,6 @@ import arrow.core.Validated.Valid import arrow.core.compose import arrow.core.identity import arrow.typeclasses.Monoid -import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -66,13 +65,9 @@ public interface PIso : PPrism, PLens, Gette override fun modify(source: S, map: (focus: A) -> B): T = reverseGet(map(get(source))) - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = map(get(source)) - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - map(get(source)) - /** * Reverse a [PIso]: the source becomes the target and the target becomes the source */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt index b57c701f759..6912a35d6bd 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Lens.kt @@ -4,7 +4,6 @@ import arrow.core.Either import arrow.core.NonEmptyList import arrow.core.identity import arrow.typeclasses.Monoid -import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -39,13 +38,9 @@ public interface PLens : Getter, POptional, PSette override fun getOrModify(source: S): Either = Either.Right(get(source)) - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = map(get(source)) - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - map(get(source)) - /** * Join two [PLens] with the same focus in [A] */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt index 50fc654048c..5ccbb8d4b54 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Optional.kt @@ -10,7 +10,6 @@ import arrow.core.identity import arrow.core.prependTo import arrow.core.toOption import arrow.typeclasses.Monoid -import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -73,14 +72,9 @@ public interface POptional : PSetter, POptionalGetter - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = getOrModify(source).map(map).getOrElse { M.empty() } - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - getOrModify(source).map(map).getOrElse { empty } - - /** * Modify the focus of a [POptional] with a function [map] */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt index b05adb11b93..efa0b790678 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/OptionalGetter.kt @@ -7,7 +7,6 @@ import arrow.core.Some import arrow.core.flatMap import arrow.core.getOrElse import arrow.typeclasses.Monoid -import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmStatic /** @@ -38,13 +37,9 @@ public interface POptionalGetter: Fold { public fun getOrNull(source: S): A? = getOrModify(source).getOrNull() - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = getOrModify(source).map(map).getOrElse { M.empty() } - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - getOrModify(source).map(map).getOrElse { empty } - /** * Join two [POptionalGetter] with the same focus */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt index b4be2ccdf2a..96e420e9415 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/Prism.kt @@ -10,7 +10,6 @@ import arrow.core.identity import arrow.core.left import arrow.core.right import arrow.typeclasses.Monoid -import arrow.typeclasses.MonoidDeprecation import kotlin.jvm.JvmName import kotlin.jvm.JvmStatic @@ -42,13 +41,9 @@ public interface PPrism : POptional, PSetter public fun reverseGet(focus: B): T - @Deprecated(MonoidDeprecation, ReplaceWith("foldMap(empty, {r1, r2 -> r1 + r2}, source, map)", "arrow.optics.foldMap")) override fun foldMap(M: Monoid, source: S, map: (focus: A) -> R): R = getOrNull(source)?.let(map) ?: M.empty() - override fun foldMap(empty: R, combine: (R, R) -> R, source: S, map: (focus: A) -> R): R = - getOrNull(source)?.let(map) ?: empty - /** * Modify the focus of a [PPrism] with a function */ diff --git a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt index 042ee01fbc8..16c0d920bc6 100644 --- a/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt +++ b/arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/typeclasses/FilterIndex.kt @@ -43,9 +43,6 @@ public fun interface FilterIndex { override fun modify(source: List, map: (focus: A) -> A): List = source.mapIndexed { index, a -> if (p(index)) map(a) else a } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: List, map: (focus: A) -> R): R = - source.foldIndexed(empty) { index, acc, a -> if (p(index)) combine(acc, map(a)) else acc } } } @@ -61,9 +58,6 @@ public fun interface FilterIndex { override fun modify(source: Map, map: (focus: V) -> V): Map = source.mapValues { (k, v) -> if (p(k)) map(v) else v } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Map, map: (focus: V) -> R): R = - source.entries.fold(empty) { acc, (k, v) -> if (p(k)) combine(acc, map(v)) else acc } } } @@ -83,9 +77,6 @@ public fun interface FilterIndex { override fun modify(source: NonEmptyList, map: (focus: A) -> A): NonEmptyList = source.mapIndexed { index, a -> if (p(index)) map(a) else a }.toNonEmptyListOrNull() ?: throw IndexOutOfBoundsException("Empty list doesn't contain element at index 0.") - - override fun foldMap(empty: R, combine: (R, R) -> R, source: NonEmptyList, map: (focus: A) -> R): R = - source.foldIndexed(empty) { index, acc, r -> if (p(index)) combine(acc, map(r)) else acc } } } @@ -101,9 +92,6 @@ public fun interface FilterIndex { override fun modify(source: Sequence, map: (focus: A) -> A): Sequence = source.mapIndexed { index, a -> if (p(index)) map(a) else a } - - override fun foldMap(empty: R, combine: (R, R) -> R, source: Sequence, map: (focus: A) -> R): R = - source.foldIndexed(empty) { index, acc, a -> if (p(index)) combine(acc, map(a)) else acc } } } From 6a56a8c71557e44370b9c54e50e0655a027b300a Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Wed, 29 Mar 2023 09:40:58 +0200 Subject: [PATCH 5/9] Fix combine 2 eithers test --- .../arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt index 06616887d60..5eb758e859b 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt @@ -456,12 +456,12 @@ class EitherTest : StringSpec({ val obtained = e1.combine(e2, { l1, l2 -> l1 + l2 }, { r1, r2 -> r1 + r2 }) val expected = when(e1){ is Left -> when(e2) { - is Left -> e1.value + e2.value + is Left -> Left(e1.value + e2.value) is Right -> e1 } is Right -> when(e2) { is Left -> e2 - is Right -> e1.value + e2.value + is Right -> Right(e1.value + e2.value) } } obtained shouldBe expected From 3242ae41e113b42397056801e43764a84d19004b Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Wed, 29 Mar 2023 09:41:14 +0200 Subject: [PATCH 6/9] Fix combineAll test --- .../arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt index 5eb758e859b..60cbd86a77f 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/EitherTest.kt @@ -469,7 +469,7 @@ class EitherTest : StringSpec({ } - "combine should combine list of eithers " { + "combineAll replacement should work " { checkAll(Arb.list(Arb.either(Arb.string(), Arb.int()))) { list -> val obtained = list.fold, Either>(0.right()) { x, y -> Either.zipOrAccumulate( @@ -478,7 +478,7 @@ class EitherTest : StringSpec({ y, { b1, b2 -> b1 + b2 }) } - val expected = list[0] + val expected = list.combineAll(Monoid.string(), Monoid.int()) obtained shouldBe expected } } From 53cd889015d3273ac3e2c9f85f0a7c8f58e73433 Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Wed, 29 Mar 2023 13:12:18 +0200 Subject: [PATCH 7/9] Replace missed combine in deprecations --- .../commonMain/kotlin/arrow/core/Either.kt | 2 +- .../commonMain/kotlin/arrow/core/Iterable.kt | 2 +- .../kotlin/arrow/core/NonEmptyList.kt | 4 +- .../commonMain/kotlin/arrow/core/Validated.kt | 63 ++++++++----------- .../src/commonMain/kotlin/arrow/core/map.kt | 6 +- 5 files changed, 32 insertions(+), 45 deletions(-) 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 06da8b8ace3..81679581962 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 @@ -904,7 +904,7 @@ public sealed class Either { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith(" fold({ empty }, f)") + ReplaceWith("fold({ empty }, f)") ) public fun foldMap(MN: Monoid, f: (B) -> C): C = fold({ MN.empty() }, f) 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 ddc2af3b260..0492e1986d8 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 @@ -928,7 +928,7 @@ public fun Iterable>.unalign(): Pair, List> = separa public inline fun Iterable.unalign(fa: (C) -> Ior): Pair, List> = map(fa).unalign() -@Deprecated("Use fold from Kotlin Std instead", ReplaceWith("fold(initialValue, {a1, a2 -> a1 + a2})")) +@Deprecated("Use fold from Kotlin Std instead", ReplaceWith("this.fold(initialValue){a1, a2 -> a1 + a2}")) public fun Iterable.combineAll(MA: Monoid): A = fold(MA.empty(), MA::combine) diff --git a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt index 1a763ab77d4..7d204e488e6 100644 --- a/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt +++ b/arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/NonEmptyList.kt @@ -481,7 +481,7 @@ public inline fun NonEmptyList.traverse( @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "this.mapOrAccumulate({ a, b -> a + b }) { it.bind() }.toValidated()", + "this.mapOrAccumulate({ e1, e2 -> e1 + e2 }) { it.bind() }.toValidated()", "arrow.core.mapOrAccumulate" ) ) @@ -491,7 +491,7 @@ public fun NonEmptyList>.sequenceValidated(semigroup: Sem @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "this.mapOrAccumulate({ a, b -> a + b }) { it.bind() }.toValidated()", + "this.mapOrAccumulate({ e1, e2 -> e1 + e2 }) { it.bind() }.toValidated()", "arrow.core.mapOrAccumulate" ) ) 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 710f5166612..1a4b0a230df 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 @@ -559,9 +559,8 @@ public sealed class Validated { @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), fb.toEither(), ::Pair).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), fb.toEither(), ::Pair).toValidated()", + "arrow.core.Either" ) ) public fun Validated.zip(SE: Semigroup, fb: Validated): Validated> = @@ -570,9 +569,8 @@ public fun Validated.zip(SE: Semigroup, fb: Validated): @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -585,9 +583,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -601,9 +598,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -618,9 +614,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -637,9 +632,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -665,9 +659,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -695,9 +688,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), h.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), h.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -727,9 +719,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), h.toEither(), i.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), h.toEither(), i.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -761,9 +752,8 @@ public inline fun Validated.zip( @Deprecated( ValidatedDeprMsg + "zipOrAccumulate for Either now exposes this same functionality", ReplaceWith( - "Either.zipOrAccumulate(SE::combine, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), h.toEither(), i.toEither(), j.toEither(), f).toValidated()", - "arrow.core.Either", - "arrow.typeclasses.combine" + "Either.zipOrAccumulate({ e1, e2 -> e1 + e2 }, toEither(), b.toEither(), c.toEither(), d.toEither(), e.toEither(), ff.toEither(), g.toEither(), h.toEither(), i.toEither(), j.toEither(), f).toValidated()", + "arrow.core.Either" ) ) public inline fun Validated.zip( @@ -981,8 +971,7 @@ public fun Validated.leftWiden(): Validated = @Deprecated( DeprAndNicheMsg + "Prefer using the Either DSL, or map", ReplaceWith( - "(0 until (n.coerceAtLeast(0))).mapOrAccumulate(SE::combine) { bind() }.toValidated()", - "arrow.typeclasses.combine" + "(0 until (n.coerceAtLeast(0))).mapOrAccumulate({ e1, e2 -> e1 + e2 }) { bind() }.toValidated()" ) ) public fun Validated.replicate(SE: Semigroup, n: Int): Validated> = @@ -1160,9 +1149,8 @@ public inline fun Validated.valueOr(f: (E) -> A): A = @Deprecated( DeprAndNicheMsg + "Use recover on Either after refactoring", ReplaceWith( - "toEither().recover { e -> that().mapLeft { ee -> SE.combine(e, ee) }.bind() }.toValidated()", - "arrow.core.recover", - "arrow.typeclasses.combine" + "toEither().recover { e -> that().mapLeft { ee -> e + ee }.bind() }.toValidated()", + "arrow.core.recover" ) ) public inline fun Validated.findValid(SE: Semigroup, that: () -> Validated): Validated = @@ -1267,9 +1255,8 @@ public fun Validated.combine( @Deprecated( DeprAndNicheMsg, ReplaceWith( - "toEither().recover { e -> y.toEither().recover { ee -> raise(SE.combine(e, ee)) }.bind() }.toValidated()", - "arrow.core.recover", - "arrow.typeclasses.combine" + "toEither().recover { e -> y.toEither().recover { ee -> raise { e + ee }} }.bind() }.toValidated()", + "arrow.core.recover" ) ) public fun Validated.combineK(SE: Semigroup, y: Validated): Validated = 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 f9a85c38c54..376dd56f2d3 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 @@ -301,7 +301,7 @@ public fun Map>.sequenceEither(): Either> @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "mapOrAccumulate(semigroup::combine) { f(it.value).bind() }.toValidated()", + "mapOrAccumulate({e1, e2 -> e1 + e2}) { f(it.value).bind() }.toValidated()", "arrow.core.mapOrAccumulate", "arrow.typeclasses.combine" ) @@ -353,7 +353,7 @@ public inline fun Map.mapOrAccumulate( @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "mapOrAccumulate, A>(semigroup::combine) { it.value.bind() }.toValidated()", + "mapOrAccumulate, A>({e1, e2 -> e1 + e2}) { it.value.bind() }.toValidated()", "arrow.core.mapOrAccumulate", "arrow.typeclasses.combine" ) @@ -364,7 +364,7 @@ public fun Map>.sequenceValidated(semigroup: Semigr @Deprecated( ValidatedDeprMsg + "Use the mapOrAccumulate API instead", ReplaceWith( - "mapOrAccumulate, A>(semigroup::combine) { it.value.bind() }.toValidated()", + "mapOrAccumulate, A>({e1, e2 -> e1 + e2}) { it.value.bind() }.toValidated()", "arrow.core.mapOrAccumulate", "arrow.typeclasses.combine" ) From 44cfd79d32dbdbe4b34e44352dff815a4198cf84 Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Thu, 30 Mar 2023 12:29:45 +0200 Subject: [PATCH 8/9] Replace missed combine methods --- .../arrow-core/src/commonMain/kotlin/arrow/core/map.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 376dd56f2d3..5ff873c1033 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 @@ -506,7 +506,7 @@ public fun Map.salign(other: Map, combine: (A, A) -> A): Map< @Deprecated( "${SemigroupDeprecation}\n use align instead", - ReplaceWith("salign(other, SG::combine)", "arrow.typeclasses.combine") + ReplaceWith("salign(other){a1, a2 -> a1 + a2}") ) public fun Map.salign(SG: Semigroup, other: Map): Map = salign(other, SG::combine) @@ -656,16 +656,15 @@ public fun Map.combine(other: Map, combine: (A, A) -> A): Map if (size < other.size) fold(other) { my, (k, b) -> my + Pair(k, my[k]?.let { combine(b, it) } ?: b) } else other.fold(this@combine) { my, (k, a) -> my + Pair(k, my[k]?.let { combine(a, it) } ?: a) } -@Deprecated(SemigroupDeprecation, ReplaceWith("combine(b, SG::combine)", "arrow.typeclasses.combine")) +@Deprecated(SemigroupDeprecation, ReplaceWith("combine(b){a1, a2 -> a1 + a2}")) public fun Map.combine(SG: Semigroup, b: Map): Map = combine(b, SG::combine) @Deprecated( "Use fold & Map.combine instead.\n$NicheAPI", ReplaceWith( - "fold(emptyMap()) { acc, map -> acc.combine(map, SG::combine) }", - "arrow.core.combine", - "arrow.typeclasses.combine" + "fold(emptyMap()) { acc, map -> acc.combine(map){a1, a2 -> a1 + a2} }", + "arrow.core.combine" ) ) public fun Iterable>.combineAll(SG: Semigroup): Map = From 7976d99690d2ede85d487cbd086b6ad8d5e4c5f5 Mon Sep 17 00:00:00 2001 From: Jose Gutierrez Date: Thu, 30 Mar 2023 12:35:27 +0200 Subject: [PATCH 9/9] Rename replaced fold parameters --- .../arrow-core/src/commonMain/kotlin/arrow/core/Either.kt | 4 ++-- .../core/arrow-core/src/commonMain/kotlin/arrow/core/Ior.kt | 2 +- .../arrow-core/src/commonMain/kotlin/arrow/core/Iterable.kt | 4 ++-- .../arrow-core/src/commonMain/kotlin/arrow/core/Option.kt | 4 ++-- .../src/commonMain/kotlin/arrow/core/Validated.kt | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) 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 0a3945a656e..a88ec1907e7 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 @@ -904,7 +904,7 @@ public sealed class Either { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith("fold({ empty }, f)") + ReplaceWith("fold({ ifLeft }, f)") ) public fun foldMap(MN: Monoid, f: (B) -> C): C = fold({ MN.empty() }, f) @@ -2551,7 +2551,7 @@ public inline fun Either.zip( @Deprecated( NicheAPI + "Prefer using the Either DSL, or map", - ReplaceWith("if (n <= 0) Either.Right(empty) else this.map { b -> List(n) { b }.fold(empty){r, t -> r + t} }") + ReplaceWith("if (n <= 0) Either.Right(initial) else this.map { b -> List(n) { b }.fold(initial){r, t -> r + t} }") ) public fun Either.replicate(n: Int, MB: Monoid): Either = map { b -> List(n) { b }.fold(MB.empty(), MB::combine) } 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 b1c8666fcdf..bb13c2565de 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 @@ -242,7 +242,7 @@ public sealed class Ior { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith("this.fold({ empty }, { f }, { _, b -> f(b) })") + ReplaceWith("this.fold({ leftValue }, { f }, { _, b -> f(b) })") ) public inline fun foldMap(MN: Monoid, f: (B) -> C): C { contract { callsInPlace(f, InvocationKind.AT_MOST_ONCE) } 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 0492e1986d8..7115dc3f83b 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 @@ -928,7 +928,7 @@ public fun Iterable>.unalign(): Pair, List> = separa public inline fun Iterable.unalign(fa: (C) -> Ior): Pair, List> = map(fa).unalign() -@Deprecated("Use fold from Kotlin Std instead", ReplaceWith("this.fold(initialValue){a1, a2 -> a1 + a2}")) +@Deprecated("Use fold from Kotlin Std instead", ReplaceWith("this.fold(initial){a1, a2 -> a1 + a2}")) public fun Iterable.combineAll(MA: Monoid): A = fold(MA.empty(), MA::combine) @@ -1239,7 +1239,7 @@ public fun List.widen(): List = public fun Iterable.fold(MA: Monoid): A = fold(MA.empty(), MA::combine) -@Deprecated(MonoidDeprecation, ReplaceWith("this.fold(empty) { acc, a -> combine(acc, f(a)) }")) +@Deprecated(MonoidDeprecation, ReplaceWith("this.fold(initial) { acc, a -> combine(acc, f(a)) }")) public fun Iterable.foldMap(MB: Monoid, f: (A) -> B): B = fold(MB.empty()) { acc, a -> MB.run { acc.combine(f(a)) } } 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 d8adcc931cc..3d2c359231e 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 @@ -1057,7 +1057,7 @@ public sealed class Option { @Deprecated( NicheAPI + "Prefer when or fold instead", - ReplaceWith("fold({ empty }, f)") + ReplaceWith("fold({ ifEmpty }, f)") ) public inline fun foldMap(MB: Monoid, f: (A) -> B): B = fold({ MB.empty() }, f) @@ -1417,7 +1417,7 @@ public inline fun Option.redeemWith(fe: (Unit) -> Option, fb: (A) - @Deprecated( NicheAPI + "Prefer using the Option DSL or map", - ReplaceWith("this.map { a -> List(n) { a }.fold(intialValue){a1, a2 -> a1 + a2} }") + ReplaceWith("this.map { a -> List(n) { a }.fold(initial){a1, a2 -> a1 + a2} }") ) public fun Option.replicate(n: Int, MA: Monoid): Option = map { a -> List(n) { a }.fold(MA.empty(), MA::combine) } 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 04f361cb3ac..baef295a4b4 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 @@ -306,7 +306,7 @@ public sealed class Validated { @Deprecated( ValidatedDeprMsg + "Use fold on Either after refactoring instead", - ReplaceWith("toEither().fold({ initial }, f)") + ReplaceWith("toEither().fold({ invalidValue }, f)") ) public inline fun foldMap(MB: Monoid, f: (A) -> B): B = fold({ MB.empty() }, f) @@ -1030,14 +1030,14 @@ public fun Validated.bisequenceNullable(): Validated? = @Deprecated( "$MonoidDeprecation\n$DeprAndNicheMsg\nUse fold on Either after refactoring", - ReplaceWith("fold({ initial }, ::identity)") + ReplaceWith("fold({ invalidValue }, ::identity)") ) public fun Validated.fold(MA: Monoid): A = fold({ MA.empty() }, ::identity) @Deprecated( "$MonoidDeprecation\n$DeprAndNicheMsg\nUse fold on Either after refactoring", - ReplaceWith("fold({ initial }, ::identity)", "arrow.core.fold") + ReplaceWith("fold({ invalidValue }, ::identity)", "arrow.core.fold") ) public fun Validated.combineAll(MA: Monoid): A = fold(MA)