all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
53
Task/Y-combinator/Java/y-combinator-1.java
Normal file
53
Task/Y-combinator/Java/y-combinator-1.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
interface Function<A, B> {
|
||||
public B call(A x);
|
||||
}
|
||||
|
||||
public class YCombinator {
|
||||
interface RecursiveFunc<F> extends Function<RecursiveFunc<F>, F> { }
|
||||
|
||||
public static <A,B> Function<A,B> fix(final Function<Function<A,B>, Function<A,B>> f) {
|
||||
RecursiveFunc<Function<A,B>> r =
|
||||
new RecursiveFunc<Function<A,B>>() {
|
||||
public Function<A,B> call(final RecursiveFunc<Function<A,B>> w) {
|
||||
return f.call(new Function<A,B>() {
|
||||
public B call(A x) {
|
||||
return w.call(w).call(x);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
return r.call(r);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Function<Integer,Integer>, Function<Integer,Integer>> almost_fib =
|
||||
new Function<Function<Integer,Integer>, Function<Integer,Integer>>() {
|
||||
public Function<Integer,Integer> call(final Function<Integer,Integer> f) {
|
||||
return new Function<Integer,Integer>() {
|
||||
public Integer call(Integer n) {
|
||||
if (n <= 2) return 1;
|
||||
return f.call(n - 1) + f.call(n - 2);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Function<Function<Integer,Integer>, Function<Integer,Integer>> almost_fac =
|
||||
new Function<Function<Integer,Integer>, Function<Integer,Integer>>() {
|
||||
public Function<Integer,Integer> call(final Function<Integer,Integer> f) {
|
||||
return new Function<Integer,Integer>() {
|
||||
public Integer call(Integer n) {
|
||||
if (n <= 1) return 1;
|
||||
return n * f.call(n - 1);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
Function<Integer,Integer> fib = fix(almost_fib);
|
||||
Function<Integer,Integer> fac = fix(almost_fac);
|
||||
|
||||
System.out.println("fib(10) = " + fib.call(10));
|
||||
System.out.println("fac(10) = " + fac.call(10));
|
||||
}
|
||||
}
|
||||
23
Task/Y-combinator/Java/y-combinator-2.java
Normal file
23
Task/Y-combinator/Java/y-combinator-2.java
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
public class YCombinator {
|
||||
interface RecursiveFunc<F> extends Function<RecursiveFunc<F>, F> { }
|
||||
public static <A,B> Function<A,B> fix(Function<Function<A,B>, Function<A,B>> f) {
|
||||
RecursiveFunc<Function<A,B>> r = w -> f.apply(x -> w.apply(w).apply(x));
|
||||
return r.apply(r);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Integer,Integer> fib = fix(f -> n -> {
|
||||
if (n <= 2) return 1;
|
||||
return f.apply(n - 1) + f.apply(n - 2);
|
||||
});
|
||||
Function<Integer,Integer> fac = fix(f -> n -> {
|
||||
if (n <= 1) return 1;
|
||||
return n * f.apply(n - 1);
|
||||
});
|
||||
|
||||
System.out.println("fib(10) = " + fib.apply(10));
|
||||
System.out.println("fac(10) = " + fac.apply(10));
|
||||
}
|
||||
}
|
||||
142
Task/Y-combinator/Java/y-combinator-3.java
Normal file
142
Task/Y-combinator/Java/y-combinator-3.java
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
interface Function<INPUT, OUTPUT> {
|
||||
public static final List<Void> NIL = Collections.emptyList();
|
||||
public OUTPUT call(List<? extends INPUT> input);
|
||||
}
|
||||
|
||||
class Functions {
|
||||
public static <OUTPUT> OUTPUT call(
|
||||
Function<Void, OUTPUT> f) {
|
||||
return f.call(Function.NIL);
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> OUTPUT call(
|
||||
Function<INPUT, OUTPUT> f,
|
||||
INPUT input) {
|
||||
return f.call(Collections.singletonList(input));
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> OUTPUT call(
|
||||
Function<INPUT, OUTPUT> f,
|
||||
INPUT... input) {
|
||||
return f.call(Arrays.asList(input));
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> OUTPUT call(
|
||||
Function<INPUT, OUTPUT> f,
|
||||
Class<INPUT> type,
|
||||
INPUT... input) {
|
||||
List<INPUT> i = Collections.checkedList(new ArrayList<INPUT>(), type);
|
||||
i.addAll(Arrays.asList(input));
|
||||
return f.call(i);
|
||||
}
|
||||
|
||||
public static <T> T input(
|
||||
List<T> input, int index) {
|
||||
return input.size() > index
|
||||
? input.get(index)
|
||||
: null;
|
||||
}
|
||||
|
||||
public static <INPUT, INPUT_OUTPUT, OUTPUT> Function<INPUT, OUTPUT> compose(
|
||||
final Function<INPUT_OUTPUT, OUTPUT> f
|
||||
, final Function<INPUT, INPUT_OUTPUT> g) {
|
||||
return new Function<INPUT, OUTPUT>() {
|
||||
@Override
|
||||
public OUTPUT call(List<? extends INPUT> input) {
|
||||
return f.call(Collections.singletonList(g.call(input)));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> Function<INPUT, OUTPUT> y(
|
||||
final Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>> f) {
|
||||
return new Function<INPUT, OUTPUT>() {
|
||||
@Override
|
||||
public OUTPUT call(List<? extends INPUT> input) {
|
||||
return Functions.call(f, new Function<INPUT, OUTPUT>() {
|
||||
@Override
|
||||
public OUTPUT call(List<? extends INPUT> input) {
|
||||
return y(f).call(input);
|
||||
}
|
||||
}).call(input);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public class Y {
|
||||
public static BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Number, Number> fibonacci = Functions.y(
|
||||
new Function<Function<Number, Number>, Function<Number, Number>>() {
|
||||
@Override
|
||||
public Function<Number, Number> call(List<? extends Function<Number, Number>> input) {
|
||||
final Function<Number, Number> f = Functions.input(input, 0);
|
||||
return new Function<Number, Number>() {
|
||||
@Override
|
||||
public Number call(List<? extends Number> input) {
|
||||
BigInteger n = new BigInteger(Functions.input(input, 0).toString());
|
||||
if (n.compareTo(TWO) <= 0) return 1;
|
||||
return new BigInteger(Functions.call(f, n.subtract(BigInteger.ONE)).toString())
|
||||
.add(new BigInteger(Functions.call(f, n.subtract(TWO)).toString()));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Function<Number, Number> factorial = Functions.y(
|
||||
new Function<Function<Number, Number>, Function<Number, Number>>() {
|
||||
@Override
|
||||
public Function<Number, Number> call(List<? extends Function<Number, Number>> input) {
|
||||
final Function<Number, Number> f = Functions.input(input, 0);
|
||||
return new Function<Number, Number>() {
|
||||
@Override
|
||||
public Number call(List<? extends Number> input) {
|
||||
BigInteger n = new BigInteger(Functions.input(input, 0).toString());
|
||||
if (n.compareTo(BigInteger.ONE) <= 0) return 1;
|
||||
return n.multiply(
|
||||
new BigInteger(Functions.call(f, n.subtract(BigInteger.ONE)).toString())
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Function<Number, Number> ackermann = Functions.y(
|
||||
new Function<Function<Number, Number>, Function<Number, Number>>() {
|
||||
@Override
|
||||
public Function<Number, Number> call(List<? extends Function<Number, Number>> input) {
|
||||
final Function<Number, Number> f = Functions.input(input, 0);
|
||||
return new Function<Number, Number>() {
|
||||
@Override
|
||||
public Number call(List<? extends Number> input) {
|
||||
BigInteger m = new BigInteger(Functions.input(input, 0) + "");
|
||||
BigInteger n = new BigInteger(Functions.input(input, 1) + "");
|
||||
return m.equals(BigInteger.ZERO)
|
||||
? n.add(BigInteger.ONE)
|
||||
: Functions.call(f, m.subtract(BigInteger.ONE),
|
||||
n.equals(BigInteger.ZERO)
|
||||
? BigInteger.ONE
|
||||
: Functions.call(f, m, n.subtract(BigInteger.ONE)));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
System.out.println("fibonacci(10) = " + Functions.call(fibonacci, 10));
|
||||
System.out.println("factorial(10) = " + Functions.call(factorial, 10));
|
||||
System.out.println("ackermann(3, 7) = " + Functions.call(ackermann, 3, 7));
|
||||
}
|
||||
}
|
||||
147
Task/Y-combinator/Java/y-combinator-4.java
Normal file
147
Task/Y-combinator/Java/y-combinator-4.java
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@FunctionalInterface
|
||||
interface VarargFunction<INPUT, OUTPUT> {
|
||||
public OUTPUT apply(List<? extends INPUT> input);
|
||||
|
||||
public default OUTPUT apply() {
|
||||
return apply(Collections.emptyList());
|
||||
}
|
||||
|
||||
public default OUTPUT apply(INPUT input) {
|
||||
return apply(Collections.singletonList(input));
|
||||
}
|
||||
|
||||
public default OUTPUT apply(INPUT input, INPUT input2) {
|
||||
return apply(Arrays.asList(input, input2));
|
||||
}
|
||||
|
||||
public default OUTPUT apply(INPUT input, INPUT input2, INPUT input3) {
|
||||
return apply(Arrays.asList(input, input2, input3));
|
||||
}
|
||||
|
||||
public default OUTPUT apply(Class<INPUT> type, Object... input) {
|
||||
List<INPUT> i = Collections.checkedList(new ArrayList<>(), type);
|
||||
for (Object object : input) {
|
||||
i.add(type.cast(object));
|
||||
}
|
||||
return apply(i);
|
||||
}
|
||||
|
||||
public default <POST_OUTPUT> VarargFunction<INPUT, POST_OUTPUT> compose(
|
||||
VarargFunction<OUTPUT, POST_OUTPUT> after) {
|
||||
return input -> after.apply(apply(input));
|
||||
}
|
||||
|
||||
public default Function<INPUT, OUTPUT> toFunction() {
|
||||
return input -> apply(input);
|
||||
}
|
||||
|
||||
public default BiFunction<INPUT, INPUT, OUTPUT> toBiFunction() {
|
||||
return (input, input2) -> apply(input, input2);
|
||||
}
|
||||
|
||||
public default <PRE_INPUT> VarargFunction<PRE_INPUT, OUTPUT> transformArguments(Function<PRE_INPUT, INPUT> transformer) {
|
||||
return input -> apply(input.parallelStream().map(transformer).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface SelfApplicable<OUTPUT> {
|
||||
OUTPUT apply(SelfApplicable<OUTPUT> input);
|
||||
}
|
||||
|
||||
class Utils {
|
||||
public static <T> T input( List<T> input, int index) {
|
||||
return input.size() > index ? input.get(index) : null;
|
||||
}
|
||||
|
||||
/* Based on https://gist.github.com/aruld/3965968/#comment-604392 */
|
||||
|
||||
public static <INPUT, OUTPUT> SelfApplicable<Function<Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>>, Function<INPUT, OUTPUT>>> y(Class<INPUT> input, Class<OUTPUT> output) {
|
||||
return y -> f -> x -> f.apply(y.apply(y).apply(f)).apply(x);
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> Function<Function<Function<INPUT, OUTPUT>, Function<INPUT, OUTPUT>>, Function<INPUT, OUTPUT>> fix(Class<INPUT> input, Class<OUTPUT> output) {
|
||||
return y(input, output).apply(y(input, output));
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> SelfApplicable<Function<Function<VarargFunction<INPUT, OUTPUT>, VarargFunction<INPUT, OUTPUT>>, VarargFunction<INPUT, OUTPUT>>> yVararg(Class<INPUT> input, Class<OUTPUT> output) {
|
||||
return y -> f -> x -> f.apply(y.apply(y).apply(f)).apply(x);
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> Function<Function<VarargFunction<INPUT, OUTPUT>, VarargFunction<INPUT, OUTPUT>>, VarargFunction<INPUT, OUTPUT>> fixVararg(Class<INPUT> input, Class<OUTPUT> output) {
|
||||
return yVararg(input, output).apply(yVararg(input, output));
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> VarargFunction<INPUT, OUTPUT> toVarargFunction(Function<INPUT, OUTPUT> function) {
|
||||
return input -> function.apply(Utils.input(input, 0));
|
||||
}
|
||||
|
||||
public static <INPUT, OUTPUT> VarargFunction<INPUT, OUTPUT> toVarargFunction(BiFunction<INPUT, INPUT, OUTPUT> function) {
|
||||
return input -> function.apply(Utils.input(input, 0), Utils.input(input, 1));
|
||||
}
|
||||
}
|
||||
|
||||
public class Y {
|
||||
public static final BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE);
|
||||
|
||||
public static final Function<Number, BigInteger> toBigInteger = ((Function<Number, Long>) Number::longValue).compose(BigInteger::valueOf);
|
||||
|
||||
public static void main(String[] args) {
|
||||
VarargFunction<Number, Number> fibonacci = Utils.fixVararg(Number.class, Number.class).apply(
|
||||
f -> Utils.toVarargFunction(
|
||||
toBigInteger.compose(
|
||||
n -> (n.compareTo(TWO) <= 0) ? 1
|
||||
: new BigInteger(f.apply(n.subtract(BigInteger.ONE)).toString())
|
||||
.add(new BigInteger(f.apply(n.subtract(TWO)).toString()))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
VarargFunction<Number, Number> factorial = Utils.fixVararg(Number.class, Number.class).apply(
|
||||
f -> Utils.toVarargFunction(
|
||||
toBigInteger.compose(
|
||||
n -> (n.compareTo(BigInteger.ONE) <= 0) ? 1
|
||||
: n.multiply(new BigInteger(f.apply(n.subtract(BigInteger.ONE)).toString()))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
VarargFunction<Number, Number> ackermann = Utils.fixVararg(Number.class, Number.class).apply(
|
||||
f -> Utils.toVarargFunction(
|
||||
(BigInteger m, BigInteger n) -> m.equals(BigInteger.ZERO) ? n.add(BigInteger.ONE)
|
||||
: f.apply(m.subtract(BigInteger.ONE),
|
||||
n.equals(BigInteger.ZERO)
|
||||
? BigInteger.ONE
|
||||
: f.apply(m, n.subtract(BigInteger.ONE)))
|
||||
).transformArguments(toBigInteger)
|
||||
);
|
||||
|
||||
Map<String, VarargFunction<Number, Number>> functions = new HashMap<>();
|
||||
functions.put("fibonacci", fibonacci);
|
||||
functions.put("factorial", factorial);
|
||||
functions.put("ackermann", ackermann);
|
||||
|
||||
Map<VarargFunction<Number, Number>, List<Number>> arguments = new HashMap<>();
|
||||
arguments.put(functions.get("fibonacci"), Arrays.asList(20));
|
||||
arguments.put(functions.get("factorial"), Arrays.asList(10));
|
||||
arguments.put(functions.get("ackermann"), Arrays.asList(3, 2));
|
||||
|
||||
functions.entrySet().parallelStream().map(
|
||||
entry ->
|
||||
entry.getKey() + arguments.get(entry.getValue()) + " = "
|
||||
+ entry.getValue().apply(arguments.get(entry.getValue()))
|
||||
).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
3
Task/Y-combinator/Java/y-combinator-5.java
Normal file
3
Task/Y-combinator/Java/y-combinator-5.java
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
factorial[10] = 3628800
|
||||
ackermann[3, 2] = 29
|
||||
fibonacci[20] = 6765
|
||||
Loading…
Add table
Add a link
Reference in a new issue