Last active
December 10, 2015 07:34
-
-
Save dankogai/6a4354bbfe84676adde9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func memoize<A: Hashable, R>(block:(A, (A->R))->R)->(A->R) { | |
var memo:Dictionary<A,R> = [:] | |
var result:(A->R)! | |
result = { | |
if let l = memo[$0] { return l } | |
let r = block($0, result) | |
memo[$0] = r | |
return r | |
} | |
return result | |
} | |
// let fib = memoize { n, f in n <= 1 ? 1 : f(n-2)+f(n-1) } | |
let fib = memoize { $0 <= 1 ? 1 : $1($0-2)+$1($0-1) } | |
for i in 0...63 { print(fib(i)) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment