Memo<T, A> constructor

Memo<T, A>(
  1. FunctionArg<T, A> computeValue, [
  2. MemoInterceptor<T, A>? interceptor
])

A class callable that is used for memoizing values(T) returned by a calcutate function(calculateValue).

This is a factorial example:

late final factorial = Memo(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'
  );
}

Implementation

Memo(
  FunctionArg<T, A> computeValue, [
  MemoInterceptor<T, A>? interceptor,
])  : _computeValue = computeValue,
      _interceptor = interceptor;