Skip to content

Commit

Permalink
Weaken Either monoid dependencies to only require a semigroup on left (
Browse files Browse the repository at this point in the history
…#2845)

* Weaken Either monoid dependencies to require only a semigroup on left

* Update API files

Co-authored-by: Michael Mroz <[email protected]>
Co-authored-by: Alejandro Serrano <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2022
1 parent f8ca4af commit 0bb0973
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,7 @@ public abstract interface class arrow/typeclasses/Monoid : arrow/typeclasses/Sem
public abstract fun combineAll (Ljava/util/Collection;)Ljava/lang/Object;
public abstract fun combineAll (Ljava/util/List;)Ljava/lang/Object;
public static fun constant (Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid;
public static fun either (Larrow/typeclasses/Monoid;Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid;
public static fun either (Larrow/typeclasses/Semigroup;Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid;
public abstract fun empty ()Ljava/lang/Object;
public static fun endo ()Larrow/typeclasses/Monoid;
public abstract fun fold (Ljava/util/Collection;)Ljava/lang/Object;
Expand All @@ -3128,7 +3128,7 @@ public final class arrow/typeclasses/Monoid$Companion {
public final fun Long ()Larrow/typeclasses/Monoid;
public final fun Short ()Larrow/typeclasses/Monoid;
public final fun constant (Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid;
public final fun either (Larrow/typeclasses/Monoid;Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid;
public final fun either (Larrow/typeclasses/Semigroup;Larrow/typeclasses/Monoid;)Larrow/typeclasses/Monoid;
public final fun endo ()Larrow/typeclasses/Monoid;
public final fun list ()Larrow/typeclasses/Monoid;
public final fun map (Larrow/typeclasses/Semigroup;)Larrow/typeclasses/Monoid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public interface Monoid<A> : Semigroup<A> {
public fun string(): Monoid<String> = StringMonoid

@JvmStatic
public fun <A, B> either(MA: Monoid<A>, MB: Monoid<B>): Monoid<Either<A, B>> =
EitherMonoid(MA, MB)
public fun <A, B> either(SGA: Semigroup<A>, MB: Monoid<B>): Monoid<Either<A, B>> =
EitherMonoid(SGA, MB)

@JvmStatic
public fun <A> endo(): Monoid<Endo<A>> =
Expand Down Expand Up @@ -191,22 +191,22 @@ public interface Monoid<A> : Semigroup<A> {
}

private class EitherMonoid<L, R>(
private val MOL: Monoid<L>,
private val SGOL: Semigroup<L>,
private val MOR: Monoid<R>
) : Monoid<Either<L, R>> {
override fun empty(): Either<L, R> = Either.Right(MOR.empty())

override fun Either<L, R>.combine(b: Either<L, R>): Either<L, R> =
combine(MOL, MOR, b)
combine(SGOL, MOR, b)

override fun Collection<Either<L, R>>.fold(): Either<L, R> =
fold(either(MOL, MOR))
fold(either(SGOL, MOR))

override fun fold(elems: List<Either<L, R>>): Either<L, R> =
elems.fold(either(MOL, MOR))
elems.fold(either(SGOL, MOR))

override fun Either<L, R>.maybeCombine(b: Either<L, R>?): Either<L, R> =
b?.let { combine(MOL, MOR, it) } ?: this
b?.let { combine(SGOL, MOR, it) } ?: this
}

private class PairMonoid<A, B>(
Expand Down

0 comments on commit 0bb0973

Please sign in to comment.