LanguageExt.Core

LanguageExt.Core Prelude Memoizing

Contents

class Prelude Source #

Methods

method Func<A, B> memo <A, B> (Func<A, B> func) Source #

where A : notnull

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

Remarks: Thread-safe and memory-leak safe.

method Func<T, R> memoUnsafe <T, R> (Func<T, R> func) Source #

where T : notnull

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

method Seq<T> memo <T> (IEnumerable<T> seq) Source #

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.

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.

Parameters

param seq

Enumerable to memoize

returns

IEnumerable with caching properties

class MemoExtensions Source #

Methods

method Func<T> Memo <T> (this Func<T> func) Source #

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

method Func<T, R> Memo <T, R> (this Func<T, R> func) Source #

where T : notnull

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.

method Func<T, R> MemoUnsafe <T, R> (this Func<T, R> func) Source #

where T : notnull

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

method IEnumerable<T> Memo <T> (this IEnumerable<T> seq) Source #

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.

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.

Parameters

param seq

Enumerable to memoize

returns

IEnumerable with caching properties