Last active
September 30, 2015 16:42
-
-
Save Thorium/df46659348b0e2146cf6 to your computer and use it in GitHub Desktop.
Concurrent Memoization
This file contains hidden or 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 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