The following code always served me well under 1.9.6.2:
open System.Collections.Generic
let Cached f =
let cache = new Dictionary<_,_>()
let inline set a r = cache.[ a ] <- r; r
fun a ->
match cache.TryGetValue a with
| true, result -> result
| _ -> f a |> set a
let Cached2 f =
fun a b -> Cached (fun (a,b) -> f a b) (a,b)
let sq =
let sq x =
printfn "%d * %d" x x
x * x
Cached sq
sq 3 |> printfn "%d"
sq 3 |> printfn "%d"
let plus =
let plus x y =
printfn "%d + %d" x y
x + y
Cached2 plus
plus 1 3 |> printfn "%d"
plus 1 3 |> printfn "%d"
Cached still works fine in 1.9.6.16, but Cached2 doesn't. I suppose it relies on structural hashing. Has something fundamentally changed here? It definitely broke my code! Any suggestions how to fix this?
Wiebe