Skip to content

Instantly share code, notes, and snippets.

@Thorium
Last active September 30, 2015 16:42
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 Thorium/df46659348b0e2146cf6 to your computer and use it in GitHub Desktop.
Save Thorium/df46659348b0e2146cf6 to your computer and use it in GitHub Desktop.
Concurrent Memoization
module Memoize
//More generic variant of http://www.fssnip.net/c4
open System.Collections.Concurrent
let cache = ConcurrentDictionary<(string * obj) option,Lazy<obj>>()
let memoizeConcurrent (caller:string) (f: ('a -> 'b)) =
fun (x :'a) ->
(cache.GetOrAdd(Some (caller, x|>box), lazy ((f x)|>box)).Force() |> unbox) : 'b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment