我一直在試圖將我的一些代碼轉換爲純函數,以便學習如何以功能的方式使用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],