Skip to content

VoltaireApp/memoize_dart

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memoize

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

Examples

memo1, memo2, ... memo10 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> a) => a.width * a.height);

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

imemo1, imemo2, ... imemo10 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 = imemo1((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
}

memo0 caches result on first call and can be used as lazy cache:

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

void main() {
  var func = memo0(() => new Rectangle<int>(0, 0, 10, 10));
  
  print(func());
  print(func()); // cached - same object instance
}

Contributors

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 100.0%