February 2018
Intermediate to advanced
350 pages
7h 35m
English
Memoization is a technique to cache results of pure functions. A memoized function behaves as a normal function, but stores the result of previous computations associated with the parameters supplied to produce that result.
The classic example of memoization is Fibonacci:
import arrow.syntax.function.memoizeimport kotlin.system.measureNanoTimefun recursiveFib(n: Long): Long = if (n < 2) { n} else { recursiveFib(n - 1) + recursiveFib(n - 2)}fun imperativeFib(n: Long): Long { return when (n) { 0L -> 0 1L -> 1 else -> { var a = 0L var b = 1L var c = 0L for (i in 2..n) { c = a + b a = b b = c } c } }}fun main(args: Array<String>) { var lambdaFib: (Long) -> Long = { it } //Declared ahead to be used inside recursively lambdaFib =
Read now
Unlock full access