-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMemoize.swift
58 lines (48 loc) · 1.42 KB
/
Memoize.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Memoize.swift
//
// Advent of Code Tools
//
// from https://forums.swift.org/t/advent-of-code-2023/68749/58
// usage:
// let sumOfDigits = memoize(_sumOfDigits)
// func _sumOfDigits(_ n: Int) -> Int {
// guard n != 0 else { return 0 }
// return n % 10 + sumOfDigits(n / 10)
// }
@MainActor
public func memoize<In: Hashable, Out>(_ f: @MainActor @escaping (In) -> Out) -> (In) -> Out {
var memo = [In: Out]()
return {
if let result = memo[$0] {
return result
} else {
let result = f($0)
memo[$0] = result
return result
}
}
}
// or, taken from https://forums.swift.org/t/advent-of-code-2024/76301/117
// usage:
// let sumOfDigits = Memoized<Int, Int> { n, recurse in
// guard n != 0 else { return 0 }
// return n % 10 + recurse(n / 10)
// }
public final class Memoized<In: Hashable, Out> {
private var _compute: (In, Memoized) -> Out
public init(_ compute: @escaping (In, Memoized) -> Out) {
_compute = { _, _ in fatalError() }
unowned let me = self
var cache: [In: Out] = [:]
_compute = { input, _ in
if let answer = cache[input] {
return answer
}
let answer = compute(input, me)
cache[input] = answer
return answer
}
}
public func callAsFunction(_ input: In) -> Out { _compute(input, self) }
}