All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jnape.palatable.lambda.functions.specialized.Pure Maven / Gradle / Ivy

There is a newer version: 5.5.0
Show newest version
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 app) {
        return app::pure;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy