SKY

Nov. 3rd, 2016 02:57 pm
xacid: (Default)

import java.util.function.Function;
import static fun.SKY.Fun.*;

public class SKY {

	public interface Fun<A, B> extends Function<A, B> {

	    static <A> Fun<A, A> I() {
	        return a -> a;
	    }

	    static <E, A> Fun<E, A> K(final A a) {
	        return e -> a;
	    }
	    
	    static <E, A, B> Fun<E, B> S(
                    final Fun<E, Fun<A, B>> f, 
                    final Fun<E, A> a) {
	        return e -> f.apply(e).apply(a.apply(e));
	    }

            static <A,B> Fun<A,B> Y(
                    final Fun<Fun<A,B>, Fun<A,B>> f) {
                return x -> f.apply(Y(f)).apply(x);
            }

            Fun<Long, Fun<Long, Long>> PLUS = 
                 a -> b -> a + b;

            Fun<Long,Long> FACT = Y(f -> n -> 
                 (n <= 1)? 1: (n * f.apply(n - 1)));
        
	}

    public static void main(final String[] args) {
        final Fun<Long, Long> xx = S(S(K(PLUS), I()), I());
        System.out.println(FACT.compose(xx).apply(10L));
    }

}

xacid: (Default)
package fun;

import java.util.function.Function;

public interface Fun<A, B> extends Function<A, B> {
    Fun<?, ?> I = a -> a;

    static <A> Fun<A, A> I() {
        return (Fun<A, A>) I;
    }

    static <E, A> Fun<E, A> K(final A a) {
        return e -> a;
    }

    static <E, A, B> Fun<E, B> S(final Fun<E, Fun<A, B>> f, final Fun<E, A> a) {
        return e -> f.apply(e).apply(a.apply(e));
    }
}

----

package fun;

import static fun.Fun.*;

public class Funtest {
    public static void main(final String[] args) {
        
        final Fun<Integer, Fun<Integer, Integer>> PLUS = a -> b -> a + b;

        final Fun<Integer, Integer> xx = S(S(K(PLUS), I()), I());

        System.out.print(xx.apply(5));
    }
}

Profile

xacid: (Default)
xacid

June 2017

S M T W T F S
    123
45678910
11121314151617
18192021222324
252627282930 

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 26th, 2017 06:34 am
Powered by Dreamwidth Studios