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

com.jnape.palatable.lambda.functions.specialized.Lift 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.internal.Runtime;
import com.jnape.palatable.lambda.monad.MonadBase;
import com.jnape.palatable.lambda.monad.MonadRec;

import static com.jnape.palatable.lambda.functions.builtin.fn1.Downcast.downcast;

/**
 * Generalized, portable lifting operation for lifting a {@link MonadRec} into a {@link MonadBase}.
 *
 * @param  the {@link MonadBase} to lift into
 */
@FunctionalInterface
public interface Lift> {

    > MonadBase checkedApply(MonadRec ga)
            throws Throwable;

    default , MBA extends MonadBase> MBA apply(MonadRec ma) {
        try {
            return downcast(checkedApply(ma));
        } catch (Throwable t) {
            throw Runtime.throwChecked(t);
        }
    }

    /**
     * Static method to aid inference.
     *
     * @param lift the {@link Lift}
     * @param   the {@link MonadBase} to lift into
     * @return the {@link Lift}
     */
    static > Lift lift(Lift lift) {
        return lift;
    }
}