memo1<A, R> function

Func1<A, R> memo1<A, R>(
  1. Func1<A, R> func
)

Checks 1 argument for equality with == operator and returns the cached result if it was not changed.

Implementation

Func1<A, R> memo1<A, R>(Func1<A, R> func) {
  late A prevArg;
  late R prevResult;
  bool isInitial = true;

  return ((A arg) {
    if (!isInitial && arg == prevArg) {
      return prevResult;
    } else {
      prevArg = arg;
      prevResult = func(arg);
      isInitial = false;

      return prevResult;
    }
  });
}