我正在尝试在Java 1.8中实现Church Numerals。我的第一次尝试是:
import java.util.function.UnaryOperator;
@FunctionalInterface
public interface ChurchNumeral {
public static ChurchNumeral valueOf(int n) {
if (n < 0) {
throw new IllegalArgumentException("Argument n must be non-negative.");
}
if (n == 0) {
return (f, arg) -> arg;
}
return (f, arg) -> f(valueOf(n-1).apply(f, arg));
}
<T> T apply(UnaryOperator<T> f, T arg);
}
由于函数方法具有类型参数,因此失败。 (具体来说,带有lambda表达式的行会给出错误:“Illegal lambda expression:类型ChurchNumeral的方法应用是通用的”。)
根据有关泛型与功能接口使用的相关问题的答案,我尝试参数化该类:
import java.util.function.UnaryOperator;
@FunctionalInterface
public interface ChurchNumeral<T> { // This line changed.
public static ChurchNumeral<?> valueOf(int n) { // This line changed.
if (n < 0) {
throw new IllegalArgumentException("Argument n must be non-negative.");
}
if (n == 0) {
return (f, arg) -> arg;
}
return (f, arg) -> f(valueOf(n-1).apply(f, arg));
}
T apply(UnaryOperator<T> f, T arg); // This line changed.
}
第一个lambda表达式现在编译,但第二个表达式失败并出现此错误:
该方法适用于(UnaryOperator,捕获#1-of?) 类型ChurchNumeral不适用于参数 (UnaryOperator,Object)
此外,我不希望每种可能的函数/参数类型都有不同版本的ChurchNumeral.ZERO。
有什么建议吗?
答案 0 :(得分:3)
static interface Church<T> extends UnaryOperator<UnaryOperator<T>> {
static <T> Church<T> of(int n) {
if (n < 0) {
throw new IllegalArgumentException();
} else if (n == 0) {
return f -> (t -> t);
} else {
return sum(f -> f, Church.of(n-1));
}
}
static <T> Church<T> sum(Church<T> a, Church<T> b) {
return f -> b.apply(f).andThen(a.apply(f))::apply;
}
}
public static void main(String[] args) {
Church<Integer> five = Church.of(5);
Church<Integer> three = Church.of(3);
Church<Integer> eight = Church.sum(five, three);
assert 3 == three.apply(x -> x + 1).apply(0);
assert 5 == five.apply(x -> x + 1).apply(0);
assert 8 == eight.apply(x -> x + 1).apply(0);
}
修改:如果您希望教会界面中有apply(UnaryOperator<T> f, T arg)
,那么您就可以拨打.apply(x->x+1,0)
而不是.apply(x->x+1).apply(0)
,可以添加默认值像这样的方法到上面的教会界面:
default T apply(UnaryOperator<T> f, T t) {
return this.apply(f).apply(t);
}
编辑2:以下是可以在不同类型之间进行转换的更新类。我还添加了一个mul
方法来乘法,看看它是如何工作的:
static interface Church<T> extends UnaryOperator<UnaryOperator<T>> {
static <T> Church<T> of(int n) {
if (n < 0) {
throw new IllegalArgumentException();
} else if (n == 0) {
return zero();
} else {
return sum(one(), Church.of(n - 1));
}
}
static <T> Church<T> zero() {
return f -> (t -> t);
}
static <T> Church<T> one() {
return f -> f;
}
static <T> Church<T> sum(Church<T> a, Church<T> b) {
return f -> b.apply(f).andThen(a.apply(f))::apply;
}
static <T> Church<T> mul(Church<T> a, Church<T> b) {
return f -> a.apply(b.apply(f))::apply;
}
default <U> Church<U> convert() {
return (Church<U>) this;
}
}
public static void main(String[] args) {
Church<Integer> zero = Church.zero();
Church<Integer> five = Church.of(5);
Church<Integer> three = Church.of(3);
Church<Integer> eight = Church.sum(five, three);
Church<Integer> fifteen = Church.mul(three, five);
assert 0 == zero.apply(x -> x + 1).apply(0);
assert 3 == three.apply(x -> x + 1).apply(0);
assert 5 == five.apply(x -> x + 1).apply(0);
assert 8 == eight.apply(x -> x + 1).apply(0);
assert 15 == fifteen.apply(x -> x + 1).apply(0);
Church<String> strOne = Church.one();
Church<String> strThree = three.convert(); // make Church<String>
// from a Church<Integer>
assert "foo:bar".equals(strOne.apply("foo:"::concat).apply("bar"));
assert "foo:foo:foo:bar".equals(strThree.apply("foo:"::concat).apply("bar"));
}
答案 1 :(得分:2)
有没有办法做到这一点所以我不需要创建一个ChurchNumeral 每种可能的类型?我想能够申请ZERO(例如) 任何UnaryOperator和类型为T的参数
我假设你的意思是你想做这样的事情:
ChurchNumeral five = ChurchNumeral.valueOf(5);
five.apply(s -> s + s, "s");
five.apply(Math::sqrt, Double.MAX_VALUE);
表示第一个示例中的方法签名:
<T> T apply(UnaryOperator<T> f, T arg);
是需要的。
解决方法是创建一个与lambdas兼容的子接口,并将对apply
的调用委托给其方法,如下所示。
public static void main(String[]a){
ChurchNumeral five = ChurchNumeral.valueOf(5);
System.out.println(five.apply(s -> s + s, "s"));
System.out.println(five.apply(Math::sqrt, Double.MAX_VALUE));
}
@FunctionalInterface
private interface ChurchNumeralT<T> extends ChurchNumeral {
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
default<U> U apply(UnaryOperator<U> f, U arg){
return (U)((ChurchNumeralT)this).tapply(f, arg);
}
T tapply(UnaryOperator<T> f, T arg);
}
public interface ChurchNumeral {
<T> T apply(UnaryOperator<T> f, T arg);
static ChurchNumeral valueOf(int n) {
if (n < 0) {
throw new IllegalArgumentException("Argument n must be non-negative.");
}
if (n == 0) {
return (ChurchNumeralT<?>)(f, arg) -> arg;
}
return (ChurchNumeralT<?>)(f, arg) -> f.apply(valueOf(n - 1).apply(f, arg));
}
}