MemoExtensions Class

DECLARATION
public static class MemoExtensions
NAMESPACE
LanguageExt

Methods

Memo<T, R>(Func<T, R>)
SUMMARY

Returns a Func<T,R> that wraps func. Each time the resulting Func<T,R> is called with a new value, its result is memoized (cached). Subsequent calls use the memoized value.

Remarks:

Thread-safe and memory-leak safe.

PARAMETERS
func
Func<T, R>
DECLARATION
public static Func<T, R> Memo<T, R>(this Func<T, R> func)
RETURNS
Func<T, R>
Memo<T>(Func<T>)
SUMMARY

Returns a Func<T> that wraps func. The first call to the resulting Func<T> will cache the result. Subsequent calls return the cached item.

PARAMETERS
func
Func<T>
DECLARATION
public static Func<T> Memo<T>(this Func<T> func)
RETURNS
Func<T>
Memo<T>(IEnumerable<T>)
SUMMARY

Enumerable memoization. As an enumerable is enumerated each item is retained in an internal list, so that future evalation of the enumerable isn't done. Only items not seen before are evaluated.

This minimises one of the major problems with the IEnumerable / yield return pattern by causing at-most-once evaluation of each item.

Use the IEnumerable extension method Memo for convenience.

PARAMETERS
seq
IEnumerable<T>
Enumerable to memoize
REMARKS
Although this allows efficient lazy evaluation, it does come at a memory cost. Each item is cached internally, so this method doesn't allow for evaluation of infinite sequences.
DECLARATION
public static IEnumerable<T> Memo<T>(this IEnumerable<T> seq)
RETURNS
IEnumerable<T>
IEnumerable with caching properties
MemoUnsafe<T, R>(Func<T, R>)
SUMMARY

Returns a Func<T,R> that wraps func. Each time the resulting Func<T,R> is called with a new value, its result is memoized (cached). Subsequent calls use the memoized value.

Remarks:

No mechanism for freeing cached values and therefore can cause a
memory leak when holding onto the Func<T,R> reference.
Uses a ConcurrentDictionary for the cache and is thread-safe

PARAMETERS
func
Func<T, R>
DECLARATION
public static Func<T, R> MemoUnsafe<T, R>(this Func<T, R> func)
RETURNS
Func<T, R>