这个memoize函数() -> 'a
在运行时使用Null-Argument-Exception在类型的任何函数上失败.
let memoize f = let cache = System.Collections.Generic.Dictionary() fun x -> if cache.ContainsKey(x) then cache.[x] else let res = f x cache.[x] <- res res
有没有办法写一个memoize函数,也适用于() -> 'a
?
(我现在唯一的选择是使用一个Lazy
类型.调用x.Force()
来获取值.)