com.jnape.palatable.lambda.semigroup.builtin.Merge Maven / Gradle / Ivy
Show all versions of lambda Show documentation
package com.jnape.palatable.lambda.semigroup.builtin;
import com.jnape.palatable.lambda.adt.Either;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.specialized.BiSemigroupFactory;
import com.jnape.palatable.lambda.functions.specialized.SemigroupFactory;
import com.jnape.palatable.lambda.monoid.Monoid;
import com.jnape.palatable.lambda.semigroup.Semigroup;
/**
* A {@link Semigroup} instance formed by {@link Either#merge} and semigroups over L
and R
.
* Like {@link Either#merge}, this is left-biased.
*
* For the {@link Monoid}, see {@link com.jnape.palatable.lambda.monoid.builtin.Merge}.
*
* @param The left parameter type
* @param The right parameter type
* @see Monoid
* @see Either#merge
*/
public final class Merge implements BiSemigroupFactory, Semigroup, Either> {
private static final Merge INSTANCE = new Merge();
private Merge() {
}
@Override
public Semigroup> apply(Semigroup lSemigroup, Semigroup rSemigroup) {
return (x, y) -> x.merge(lSemigroup::apply, rSemigroup::apply, y);
}
@SuppressWarnings("unchecked")
public static Merge merge() {
return INSTANCE;
}
public static SemigroupFactory, Either> merge(Semigroup lSemigroup) {
return Merge.merge().apply(lSemigroup);
}
public static Semigroup> merge(Semigroup lSemigroup, Semigroup rSemigroup) {
return Merge.merge(lSemigroup).apply(rSemigroup);
}
public static Fn1, Either> merge(Semigroup lSemigroup, Semigroup rSemigroup,
Either x) {
return merge(lSemigroup, rSemigroup).apply(x);
}
public static Either merge(Semigroup lSemigroup, Semigroup rSemigroup, Either x,
Either y) {
return merge(lSemigroup, rSemigroup, x).apply(y);
}
}