com.jnape.palatable.lambda.semigroup.builtin.Absent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lambda Show documentation
Show all versions of lambda Show documentation
Functional patterns for Java
package com.jnape.palatable.lambda.semigroup.builtin;
import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functions.builtin.fn3.LiftA2;
import com.jnape.palatable.lambda.functions.specialized.SemigroupFactory;
import com.jnape.palatable.lambda.monoid.builtin.Present;
import com.jnape.palatable.lambda.semigroup.Semigroup;
/**
* A {@link Semigroup} instance formed by {@link Maybe}<A>
and a semigroup over A
. The
* application to two {@link Maybe} values is absence-biased, such that for a given {@link Maybe} x
* and y
:
*
* - if
x
is absent, the result is x
* - if
x
is present and y
is absent, the result is y
* - if both
x
and y
are present, the result is the application of the x and y values in
* terms of the provided semigroup, wrapped in {@link Maybe#just}
*
*
* @param the Maybe value parameter type
* @see Semigroup
* @see Present
* @see Maybe
*/
public final class Absent implements SemigroupFactory, Maybe> {
private static final Absent> INSTANCE = new Absent<>();
private Absent() {
}
@Override
public Semigroup> checkedApply(Semigroup aSemigroup) {
return LiftA2., Maybe>liftA2(aSemigroup)::apply;
}
@SuppressWarnings("unchecked")
public static Absent absent() {
return (Absent) INSTANCE;
}
public static Semigroup> absent(Semigroup semigroup) {
return Absent.absent().apply(semigroup);
}
public static Fn1, Maybe> absent(Semigroup aSemigroup, Maybe x) {
return absent(aSemigroup).apply(x);
}
public static Maybe absent(Semigroup semigroup, Maybe x, Maybe y) {
return absent(semigroup, x).apply(y);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy