1 people like it.
Like the snippet!
Memoization modules
Helper modules for different kinds of memoize functions.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
|
open System.Collections.Concurrent
open System.Collections.Generic
module private Memo =
let memo (cache : unit -> ('a -> 'b) -> ('a -> 'b)) f =
let cache = cache()
cache f
let memoFix cache f =
let cache = cache()
let rec fn x = cache (f fn) x
fn
module DictionaryMemo =
let createCache () =
let dict = Dictionary()
fun f x -> match dict.TryGetValue (Some x) with
| true, value -> value
| _ -> let value = f x
dict.[Some x] <- value
value
let memo f = Memo.memo createCache f
let memoFix f = Memo.memoFix createCache f
let memo2 f = memo (memo << f)
let memo3 f = memo (memo2 << f)
let memo4 f = memo (memo3 << f)
let memo5 f = memo (memo4 << f)
let memo6 f = memo (memo5 << f)
let memo7 f = memo (memo6 << f)
let memo8 f = memo (memo7 << f)
module ConcurrentMemo =
let createCache () =
let dict = ConcurrentDictionary()
fun f x -> dict.GetOrAdd(Some x, lazy (f x)).Value
let memo f = Memo.memo createCache f
let memoFix f = Memo.memoFix createCache f
let memo2 f = memo (memo << f)
let memo3 f = memo (memo2 << f)
let memo4 f = memo (memo3 << f)
let memo5 f = memo (memo4 << f)
let memo6 f = memo (memo5 << f)
let memo7 f = memo (memo6 << f)
let memo8 f = memo (memo7 << f)
|
namespace System
namespace System.Collections
namespace System.Collections.Concurrent
namespace System.Collections.Generic
val private memo : cache:(unit -> ('a -> 'b) -> 'a -> 'b) -> f:('a -> 'b) -> ('a -> 'b)
Full name: Script.Memo.memo
val cache : (unit -> ('a -> 'b) -> 'a -> 'b)
type unit = Unit
Full name: Microsoft.FSharp.Core.unit
val f : ('a -> 'b)
val cache : (('a -> 'b) -> 'a -> 'b)
val private memoFix : cache:(unit -> 'a -> 'b -> 'c) -> f:(('b -> 'c) -> 'a) -> ('b -> 'c)
Full name: Script.Memo.memoFix
val cache : (unit -> 'a -> 'b -> 'c)
val f : (('b -> 'c) -> 'a)
val cache : ('a -> 'b -> 'c)
val fn : ('b -> 'c)
val x : 'b
val createCache : unit -> (('a -> 'b) -> 'a -> 'b) (requires equality)
Full name: Script.DictionaryMemo.createCache
val dict : Dictionary<'a option,'b> (requires equality)
Multiple items
type Dictionary<'TKey,'TValue> =
new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
member Add : key:'TKey * value:'TValue -> unit
member Clear : unit -> unit
member Comparer : IEqualityComparer<'TKey>
member ContainsKey : key:'TKey -> bool
member ContainsValue : value:'TValue -> bool
member Count : int
member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
member Item : 'TKey -> 'TValue with get, set
...
nested type Enumerator
nested type KeyCollection
nested type ValueCollection
Full name: System.Collections.Generic.Dictionary<_,_>
--------------------
Dictionary() : unit
Dictionary(capacity: int) : unit
Dictionary(comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : unit
val f : ('a -> 'b) (requires equality)
val x : 'a (requires equality)
Dictionary.TryGetValue(key: 'a option, value: byref<'b>) : bool
union case Option.Some: Value: 'T -> Option<'T>
val value : 'b
val memo : f:('a -> 'b) -> ('a -> 'b) (requires equality)
Full name: Script.DictionaryMemo.memo
module Memo
from Script
val memoFix : f:(('a -> 'b) -> 'a -> 'b) -> ('a -> 'b) (requires equality)
Full name: Script.DictionaryMemo.memoFix
val f : (('a -> 'b) -> 'a -> 'b) (requires equality)
val memo2 : f:('a -> 'b -> 'c) -> ('a -> 'b -> 'c) (requires equality and equality)
Full name: Script.DictionaryMemo.memo2
val f : ('a -> 'b -> 'c) (requires equality and equality)
val memo3 : f:('a -> 'b -> 'c -> 'd) -> ('a -> 'b -> 'c -> 'd) (requires equality and equality and equality)
Full name: Script.DictionaryMemo.memo3
val f : ('a -> 'b -> 'c -> 'd) (requires equality and equality and equality)
val memo4 : f:('a -> 'b -> 'c -> 'd -> 'e) -> ('a -> 'b -> 'c -> 'd -> 'e) (requires equality and equality and equality and equality)
Full name: Script.DictionaryMemo.memo4
val f : ('a -> 'b -> 'c -> 'd -> 'e) (requires equality and equality and equality and equality)
val memo5 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f) (requires equality and equality and equality and equality and equality)
Full name: Script.DictionaryMemo.memo5
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f) (requires equality and equality and equality and equality and equality)
val memo6 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) (requires equality and equality and equality and equality and equality and equality)
Full name: Script.DictionaryMemo.memo6
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) (requires equality and equality and equality and equality and equality and equality)
val memo7 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) (requires equality and equality and equality and equality and equality and equality and equality)
Full name: Script.DictionaryMemo.memo7
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) (requires equality and equality and equality and equality and equality and equality and equality)
val memo8 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) (requires equality and equality and equality and equality and equality and equality and equality and equality)
Full name: Script.DictionaryMemo.memo8
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) (requires equality and equality and equality and equality and equality and equality and equality and equality)
module ConcurrentMemo
from Script
val createCache : unit -> (('a -> 'b) -> 'a -> 'b)
Full name: Script.ConcurrentMemo.createCache
val dict : ConcurrentDictionary<'a option,Lazy<'b>>
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<_,_>
--------------------
ConcurrentDictionary() : unit
ConcurrentDictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : unit
ConcurrentDictionary(comparer: IEqualityComparer<'TKey>) : unit
ConcurrentDictionary(concurrencyLevel: int, capacity: int) : unit
ConcurrentDictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : unit
ConcurrentDictionary(concurrencyLevel: int, collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : unit
ConcurrentDictionary(concurrencyLevel: int, capacity: int, comparer: IEqualityComparer<'TKey>) : unit
val x : 'a
ConcurrentDictionary.GetOrAdd(key: 'a option, value: Lazy<'b>) : Lazy<'b>
ConcurrentDictionary.GetOrAdd(key: 'a option, valueFactory: System.Func<'a option,Lazy<'b>>) : Lazy<'b>
val memo : f:('a -> 'b) -> ('a -> 'b)
Full name: Script.ConcurrentMemo.memo
val memoFix : f:(('a -> 'b) -> 'a -> 'b) -> ('a -> 'b)
Full name: Script.ConcurrentMemo.memoFix
val f : (('a -> 'b) -> 'a -> 'b)
val memo2 : f:('a -> 'b -> 'c) -> ('a -> 'b -> 'c)
Full name: Script.ConcurrentMemo.memo2
val f : ('a -> 'b -> 'c)
val memo3 : f:('a -> 'b -> 'c -> 'd) -> ('a -> 'b -> 'c -> 'd)
Full name: Script.ConcurrentMemo.memo3
val f : ('a -> 'b -> 'c -> 'd)
val memo4 : f:('a -> 'b -> 'c -> 'd -> 'e) -> ('a -> 'b -> 'c -> 'd -> 'e)
Full name: Script.ConcurrentMemo.memo4
val f : ('a -> 'b -> 'c -> 'd -> 'e)
val memo5 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f)
Full name: Script.ConcurrentMemo.memo5
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f)
val memo6 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g)
Full name: Script.ConcurrentMemo.memo6
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g)
val memo7 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h)
Full name: Script.ConcurrentMemo.memo7
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h)
val memo8 : f:('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i) -> ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i)
Full name: Script.ConcurrentMemo.memo8
val f : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i)
More information