Java 与Vavr的记忆似乎不一致

Java 与Vavr的记忆似乎不一致,java,vavr,Java,Vavr,当函数定义如下时 static Function1<BigInteger, BigInteger> fibonacci = Function((BigInteger value) -> value.equals(BigInteger.ZERO) ? BigInteger.ZERO : value.equals(BigInteger.ONE) ? BigInteger.ONE

当函数定义如下时

static Function1<BigInteger, BigInteger> fibonacci = Function((BigInteger value) ->
            value.equals(BigInteger.ZERO) ? BigInteger.ZERO
                    : value.equals(BigInteger.ONE) ? BigInteger.ONE
                    : value.equals(BigInteger.valueOf(2)) ? BigInteger.ONE
                    : Program.fibonacci.apply(value.subtract(BigInteger.ONE)).add(Program.fibonacci.apply(value.subtract(BigInteger.valueOf(2))))
    ).memoized();
它计算得很快。但是,如果我将
memoized()
移动到函数变量,如下所示

static Function1<BigInteger, BigInteger> fibonacci = Function((BigInteger value) ->
            value.equals(BigInteger.ZERO) ? BigInteger.ZERO
                    : value.equals(BigInteger.ONE) ? BigInteger.ONE
                    : value.equals(BigInteger.valueOf(2)) ? BigInteger.ONE
                    : Program.fibonacci.apply(value.subtract(BigInteger.ONE)).add(Program.fibonacci.apply(value.subtract(BigInteger.valueOf(2))))
    ); // Removed memoized() from here
如果未应用
memonized()
,则需要很长时间

原因可能是什么?

因为a)不会在已记录的表单上调用递归,b)记录的全部要点是您需要保存记录,而不是每次都创建一个新的记录

Program.fibonacci
是根据自身定义的,因此递归调用该版本,而不是备忘录版本。

因为a)递归不是在备忘录形式上调用的,b)备忘录的整个要点是你需要保存备忘录,而不是每次创建一个新的备忘录


Program.fibonacci
是根据自身定义的,因此递归调用该版本,而不是记忆版本。

因为a)递归不是在记忆形式上调用的,b)记忆的全部要点是你需要保存记忆,而不是每次创建一个新的记忆。我想我明白了。我的第二个例子只记了1000英镑。其他值在原始fibonacci()上调用。你能回答这个问题让我接受吗?你为什么不用比奈公式来计算斐波那契@SamirAghayarov我想这更多的是用Fibonacci作为玩具来试验函数编程,而不是真正需要函数的值。@NándorElődFekete完全正确。因为a)递归不是在记忆形式上调用的,b)记忆的全部要点是你需要保存记忆,不是每次都写新的备忘录,我想我明白了。我的第二个例子只记了1000英镑。其他值在原始fibonacci()上调用。你能回答这个问题让我接受吗?你为什么不用比奈公式来计算斐波那契@SamirAghayarov我想这更多的是用Fibonacci作为玩具来试验函数编程,而不是真正需要函数的值。
static Function1<BigInteger, BigInteger> fibonacci = Function((BigInteger value) ->
            value.equals(BigInteger.ZERO) ? BigInteger.ZERO
                    : value.equals(BigInteger.ONE) ? BigInteger.ONE
                    : value.equals(BigInteger.valueOf(2)) ? BigInteger.ONE
                    : Program.fibonacci.apply(value.subtract(BigInteger.ONE)).add(Program.fibonacci.apply(value.subtract(BigInteger.valueOf(2))))
    ); // Removed memoized() from here
fibonacci.memoized().apply(BigInteger.valueOf(1000));