Skip to content

Instantly share code, notes, and snippets.

// Naive memoize function
/// MARK - Memoize
func memo_rec<In: Hashable, Out>(yf f: ((In) -> Out) -> ((In) -> Out)) -> ((In) -> Out) {
var hash = [In: Out]()
func aux(_ x: In) -> Out {
if let hf = hash[x] {
return hf
} else {
let y = f(aux)(x) // (f aux: (In) -> Out):(In) -> Out
hash[x] = y