Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
Last active December 22, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacabraham/6549316 to your computer and use it in GitHub Desktop.
Save isaacabraham/6549316 to your computer and use it in GitHub Desktop.
Demonstrates a simple generate memoize function in F#
// Raw add function
let add (x,y) = x + y
// Memoized version
let add = memoize add
add (5,10) // Adding (5,10) to cache
add (5,10) // Cache hit for (5,10)
let memoize(code) =
let cache = Dictionary()
fun input ->
let resultIsCached,result = cache.TryGetValue(input)
if resultIsCached then
printfn "cache hit for %A" input
result
else
let result = code(input)
printfn "adding %A to cache" input
cache.[input] <- result
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment