我一直在试图将我的一些代码转换为纯函数,以便学习如何以功能的方式使用Kotlin,通过这段简单的代码片段,我想不出任何方法使我的calculateFibonacci函数成为纯函数。 我知道一个潜在的递归解决方案,但是对于潜在的堆栈溢出,Kotlin是否实现了尾部呼叫优化? 例子: val fibonacciValues = hashMapOf<Int, BigInteger>(0 to BigI
假设有一些有用的转换函数,例如random_spelling_error,我们想申请n次。 我的临时解决方案是这样的: def reapply(n, fn, arg):
for i in range(n):
arg = fn(arg)
return arg
reapply(3, random_spelling_error, "This is not a tes
如何使尾部递归下的findNextAndTail? def uncons[A](s: Seq[A]): Option[(A, Seq[A])] = for (h <- s.headOption) yield (h, s.tail)
// This works as-is. How can I make this function tail recursive?
// @tai
使用作家单子#尾递归 let inline bind ma fm =
let (Writer (a, log1)) = ma
let mb = fm a
let (Writer (b, log2)) = mb
let sum = (^w : (static member add : ^w * ^w -> ^w) (log1, log2))
Writ
我有这样定义Expr的类的一堆: sealed trait BoolExpr
sealed trait Value[T] {
def get: T
}
final case class AndExpr(left: Expr, right: Expr) extends BoolExpr
final case class EqualsExpr[T](value: Value[T],