I am making a chess engine (which heavily relies on functional programming) and it requires memoization at every step to avoid re-computation. I read this article which provided a generic function for memoization:
http://simon-fortelny.com/2017/07/04/GenericMemoization/
Code:
func memoize<T: Hashable, U>(function: @escaping (T) -> U) -> (T) -> U {
var cache : [T: U] = [:]
func memoWrapper(input: T) -> U {
if let cacheValue = cache[input] {
return cacheValue
}
let newVal = function(input)
cache[input] = newVal
return newVal
}
return memoWrapper
}
Now I want to extend that function to accept multiple input parameters. I tried using variadic arguments like this:
func memoize<T: Hashable, U>(function: @escaping (T...) -> U) -> (T...) -> U {
var cache : [[T]: U] = [:]
func memoWrapper(input: T...) -> U {
if let cacheValue = cache[input] {
return cacheValue
}
let newVal = function(input)
cache[input] = newVal
return newVal
}
return memoWrapper
}
But I'm getting 2 errors:
- Type of expression is ambiguous without more context
- Cannot pass array of type '[T]' as variadic arguments of type 'T'
Any idea what I'm doing wrong and how to make it support multiple arguments?