Skip to content

Instantly share code, notes, and snippets.

@espio999
Last active July 7, 2024 23:34
Show Gist options
  • Save espio999/82be898566c50fd65c9ec886650e4348 to your computer and use it in GitHub Desktop.
Save espio999/82be898566c50fd65c9ec886650e4348 to your computer and use it in GitHub Desktop.
F# memoize module
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