com.jnape.palatable.lambda.functions.specialized.Pure 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.functions.specialized;
import com.jnape.palatable.lambda.functor.Applicative;
import com.jnape.palatable.lambda.functor.Functor;
import com.jnape.palatable.lambda.internal.Runtime;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Downcast.downcast;
/**
* Generalized, portable {@link Applicative#pure(Object)}, with a loosened {@link Functor} constraint.
*
* @param the {@link Functor} to lift into
*/
@FunctionalInterface
public interface Pure> {
Functor checkedApply(A a) throws Throwable;
default > FA apply(A a) {
try {
@SuppressWarnings("unchecked") FA fa = downcast(checkedApply(a));
return fa;
} catch (Throwable t) {
throw Runtime.throwChecked(t);
}
}
/**
* Static method to aid inference.
*
* @param pure the {@link Pure}
* @param the {@link Functor} witness
* @return the {@link Pure}
*/
static > Pure pure(Pure pure) {
return pure;
}
/**
* Extract an {@link Applicative Applicative's} {@link Applicative#pure(Object) pure} implementation to an instance
* of {@link Pure}.
*
* @param app the {@link Applicative}
* @param the witness
* @return the {@link Pure}
*/
static > Pure of(Applicative, ? extends F> app) {
return app::pure;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy