inline<T, A> static method
FunctionMemo<T, A>
inline<T, A>(
- FunctionArg<
T, A> computeValue, [ - MemoInterceptor<
T, A> ? interceptor
Creates a memoized function version. It´s used for memoizing values(T
)
returned by a calcutate function(computeValue
).
This is a factorial example:
late final factorial = Memo.inline(calculateFactorial);
BigInt calculateFactorial(Args<int> args) {
final numero = args.arg;
if (numero == 0) return BigInt.one;
return BigInt.from(numero) * factorial(Args(numero - 1));
}
void main() {
// Returns the result of multiplication of 1 to 50.
final f50 = factorial(const Args(50));
// Returns the result immediately from cache
// because it was resolved in the previous line.
final f10 = factorial(const Args(10));
// Returns the result of the multiplication of 51 to 100
// and 50! which is obtained from the cache.
final f100 = factorial(const Args(100));
print(
'Results:\n'
'\t10!: $f10\n'
'\t50!: $f50\n'
'\t100!: $f100\n'
);
}
See also:
- Args, a class which represents the arguments received by
the function(
computeValue
), and also used as a cache value binding.
Implementation
static FunctionMemo<T, A> inline<T, A>(
FunctionArg<T, A> computeValue, [
MemoInterceptor<T, A>? interceptor,
]) =>
Memo<T, A>(computeValue, interceptor).call;