4

我正在尝试在 Java 1.8 中实现教堂数字。我的第一次尝试是:

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 表达式的行给出了错误:“非法 lambda 表达式: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 表达式现在可以编译,但第二个失败并出现以下错误:

ChurchNumeral 类型中的方法 apply(UnaryOperator, capture#1-of ?) 不适用于参数 (UnaryOperator, Object)

此外,我不想为每个可能的函数/参数类型使用不同版本的 ChurchNumeral.ZERO。

有什么建议么?

4

2 回答 2

4
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)在 Church 界面中直接调用.apply(x->x+1,0)而不是.apply(x->x+1).apply(0),你可以在上面的 Church 界面中添加一个像这样的默认方法:

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"));

}
于 2015-03-13T23:54:15.270 回答
2

有没有办法做到这一点,所以我不需要为每种可能的类型创建一个 ChurchNumeral ?我希望能够将零(例如)应用于任何 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);

是需要的。

但是,如果函数式接口中的方法具有类型参数,则不能将 lambda 表达式用于函数式接口。

一种解决方法是创建一个与 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));
    }
}
于 2015-03-13T23:17:23.870 回答