Last active
July 7, 2024 23:34
-
-
Save espio999/82be898566c50fd65c9ec886650e4348 to your computer and use it in GitHub Desktop.
F# memoize module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module memo | |
open System.Collections.Generic | |
let memoize fn = | |
//let cache = new System.Collections.Generic.Dictionary<_,_>() | |
let cache = new Dictionary<_,_>() | |
printfn "counter: %A" cache.Count | |
fun x -> | |
printfn "%A is received" x | |
match cache.TryGetValue x with | |
| true, v -> v | |
| false, _ -> let v = fn (x) | |
cache.Add(x,v) | |
v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment