memoize 1.0.0 copy "memoize: ^1.0.0" to clipboard
memoize: ^1.0.0 copied to clipboard

outdatedDart 1 only

Memoization function for dart

Memoize #

memoize returns cached result of function call when inputs were not changed from previous invocation.

Examples #

memo1 and memo2 compare arguments with == operator:

import 'dart:math';
import 'package:memoize/memoize.dart';

void main() {
  var rect1 = new Rectangle<int>(0, 0, 10, 10);
  var rect2 = new Rectangle<int>(0, 0, 10, 10);
  
  var func = memo1<Rectangle<int>, int>((Rectangle<int> a) => a.width * a.height);

   print(func(rect1));
   print(func(rect2)); // cached result is returned
}

imemo1 and imemo2 compare arguments with identical function:

import 'dart:math';
import 'package:memoize/memoize.dart';

void main() {
  var rect1 = new Rectangle<int>(0, 0, 10, 10);
  var rect2 = new Rectangle<int>(0, 0, 10, 10);
  
  var func = memo1<Rectangle<int>, int>((Rectangle<int> a) => a.width * a.height);

   print(func(rect1));
   print(func(rect1)); // cached - same object instance
   print(func(rect2)); // not cached - different object instance
}