com.jnape.palatable.lambda.semigroup.builtin.RightAny 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.SemigroupFactory;
import com.jnape.palatable.lambda.monoid.Monoid;
import com.jnape.palatable.lambda.semigroup.Semigroup;
import static com.jnape.palatable.lambda.adt.Either.right;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
/**
* A {@link Semigroup} instance formed by {@link Either}<L,R>
and a semigroup over R
.
* The application to two {@link Either} values is right-biased, such that for a given {@link Either} x
and
* y
:
*
* - if both
x
and y
are right values, the result is the application of the x and y values
* in terms of the provided semigroup, wrapped in {@link Either#right}
* - if only
x
is a right value, the result is x
* - if only
y
is a right value, the result is y
* - if neither
x
nor y
are right values, the result is y
*
*
* For the {@link Monoid}, see {@link com.jnape.palatable.lambda.monoid.builtin.RightAny}.
*
* @param The left parameter type
* @param The right parameter type
* @see Semigroup
* @see Either
*/
public final class RightAny implements SemigroupFactory, Either> {
private static final RightAny, ?> INSTANCE = new RightAny<>();
private RightAny() {
}
@Override
public Semigroup> checkedApply(Semigroup rSemigroup) {
return (x, y) -> x.match(constantly(y),
xR -> y.match(constantly(right(xR)),
rSemigroup.apply(xR).fmap(Either::right)));
}
@SuppressWarnings("unchecked")
public static RightAny rightAny() {
return (RightAny) INSTANCE;
}
public static Semigroup> rightAny(Semigroup rSemigroup) {
return RightAny.rightAny().apply(rSemigroup);
}
public static Fn1, Either> rightAny(Semigroup rSemigroup, Either x) {
return RightAny.rightAny(rSemigroup).apply(x);
}
public static Either rightAny(Semigroup rSemigroup, Either x, Either y) {
return rightAny(rSemigroup, x).apply(y);
}
}