11 people like it.

Concurrent Memoization

Memoize a function in a thread-safe manner. Added a wrapper around the value a. The idea being, that a unit value is represented as a null value at run time and as such the concurrent dictionary throws.

1: 
2: 
3: 
4: 
let memoizeConcurrent (f : 'a -> 'b) =
    let dict =
        new System.Collections.Concurrent.ConcurrentDictionary<'a, Lazy<'b>>()
    fun x -> dict.GetOrAdd(Some x, lazy (f x)).Force()
val memoizeConcurrent : f:('a -> 'b) -> ('a -> 'a0)

Full name: Script.memoizeConcurrent
val f : ('a -> 'b)
val dict : System.Collections.Concurrent.ConcurrentDictionary<'a,Lazy<'b>>
namespace System
namespace System.Collections
namespace System.Collections.Concurrent
Multiple items
type ConcurrentDictionary<'TKey,'TValue> =
  new : unit -> ConcurrentDictionary<'TKey, 'TValue> + 6 overloads
  member AddOrUpdate : key:'TKey * addValueFactory:Func<'TKey, 'TValue> * updateValueFactory:Func<'TKey, 'TValue, 'TValue> -> 'TValue + 1 overload
  member Clear : unit -> unit
  member ContainsKey : key:'TKey -> bool
  member Count : int
  member GetEnumerator : unit -> IEnumerator<KeyValuePair<'TKey, 'TValue>>
  member GetOrAdd : key:'TKey * valueFactory:Func<'TKey, 'TValue> -> 'TValue + 1 overload
  member IsEmpty : bool
  member Item : 'TKey -> 'TValue with get, set
  member Keys : ICollection<'TKey>
  ...

Full name: System.Collections.Concurrent.ConcurrentDictionary<_,_>

--------------------
System.Collections.Concurrent.ConcurrentDictionary() : unit
System.Collections.Concurrent.ConcurrentDictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>) : unit
System.Collections.Concurrent.ConcurrentDictionary(comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
System.Collections.Concurrent.ConcurrentDictionary(concurrencyLevel: int, capacity: int) : unit
System.Collections.Concurrent.ConcurrentDictionary(collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
System.Collections.Concurrent.ConcurrentDictionary(concurrencyLevel: int, collection: System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<'TKey,'TValue>>, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
System.Collections.Concurrent.ConcurrentDictionary(concurrencyLevel: int, capacity: int, comparer: System.Collections.Generic.IEqualityComparer<'TKey>) : unit
Multiple items
active recognizer Lazy: Lazy<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.( |Lazy| )

--------------------
type Lazy<'T> = System.Lazy<'T>

Full name: Microsoft.FSharp.Control.Lazy<_>
val x : 'a
System.Collections.Concurrent.ConcurrentDictionary.GetOrAdd(key: 'a, value: Lazy<'b>) : Lazy<'b>
System.Collections.Concurrent.ConcurrentDictionary.GetOrAdd(key: 'a, valueFactory: System.Func<'a,Lazy<'b>>) : Lazy<'b>
union case Option.Some: Value: 'T -> Option<'T>

More information

Link:http://fssnip.net/c4
Posted:11 years ago
Author:Matt Collins
Tags: memoize , concurrent