package me.elrod.pureio;

import java.util.function.Function;

/**
 * The identity monad and comonad, used primarily for delaying evaluation.
 */
public abstract class Identity<T> {
    public abstract T run(); // Comonad : Identity<T> -> T

    public <U> Identity<U> map(Function<T, U> f) {
        return new Identity<U>() {
            public U run() {
                return f.apply(Identity.this.run());
            }
        };
    }

    public <U> Identity<U> flatMap(Function<T, Identity<U>> f) {
        return f.apply(run());
    }
}

@@ -235,16 +235,52 @@
 %3E -%3E T%0A%0A
+    /**%0A     * Functor map.%0A     */%0A
     publ
@@ -471,16 +471,314 @@
 %0A    %7D%0A%0A
+    /**%0A     * Applicative map.%0A     */%0A    public %3CU%3E Identity%3CU%3E applyVia(Identity%3CFunction%3CT, U%3E%3E f) %7B%0A        return new Identity%3CU%3E() %7B%0A            public U run() %7B%0A                return f.run().apply(Identity.this.run());%0A            %7D%0A        %7D;%0A    %7D%0A%0A    /**%0A     * Monadic bind.%0A     */%0A
     publ

make Identity applicative