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
  • Loading branch information
Michael Mroz committed Nov 1, 2022
1 parent 96c774a commit b629907
Showing 1 changed file with 7 additions and 7 deletions.
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 b629907

Please sign in to comment.