com.jnape.palatable.lambda.monoid.builtin.LeftAny Maven / Gradle / Ivy
Show all versions of lambda Show documentation
package com.jnape.palatable.lambda.monoid.builtin;
import com.jnape.palatable.lambda.adt.Either;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.specialized.MonoidFactory;
import com.jnape.palatable.lambda.monoid.Monoid;
import com.jnape.palatable.lambda.semigroup.Semigroup;
import static com.jnape.palatable.lambda.adt.Either.left;
/**
* A {@link Monoid} instance formed by {@link Either}<L,R>
and a monoid over L
. The
* application to two {@link Either} values is left-biased, such that for a given {@link Either} x
and
* y
:
*
* - if both
x
and y
are left values, the result is the application of the x and y values
* in terms of the provided monoid, wrapped in {@link Either#left}
* - if only
x
is a left value, the result is x
* - if only
y
is a left value, the result is y
* - if neither
x
nor y
are left values, the result is y
*
*
* For the {@link Semigroup}, see {@link com.jnape.palatable.lambda.semigroup.builtin.LeftAny}.
*
* @param The left parameter type
* @param The right parameter type
* @see Monoid
* @see Either
*/
public final class LeftAny implements MonoidFactory, Either> {
private static final LeftAny, ?> INSTANCE = new LeftAny<>();
private LeftAny() {
}
@Override
public Monoid> checkedApply(Monoid lMonoid) {
Semigroup> semigroup = com.jnape.palatable.lambda.semigroup.builtin.LeftAny.leftAny(lMonoid);
return Monoid.>monoid(semigroup, () -> left(lMonoid.identity()));
}
@SuppressWarnings("unchecked")
public static LeftAny leftAny() {
return (LeftAny) INSTANCE;
}
public static Monoid> leftAny(Monoid lMonoid) {
return LeftAny.leftAny().apply(lMonoid);
}
public static Fn1, Either> leftAny(Monoid lMonoid, Either x) {
return LeftAny.leftAny(lMonoid).apply(x);
}
public static Either leftAny(Monoid lMonoid, Either x, Either y) {
return leftAny(lMonoid, x).apply(y);
}
}