memoized 1.0.2 copy "memoized: ^1.0.2" to clipboard
memoized: ^1.0.2 copied to clipboard

outdated

Useful function wrappers for avoiding unnecessary computation. it makes the function return a memoized(cached) value.

example/memoized_example.dart

import 'package:memoized/memoized.dart';

extension RangeGenExt on int {
  Iterable<int> to(int target) sync* {
    int start = this;
    while (start <= target) {
      yield start;
      start++;
    }
  }
}

extension IterableNumberExt on Iterable<int> {
  int sum() => reduce((value, element) => value + element);
}

String time<V>(V Function() fn) {
  final watch = Stopwatch()..start();
  watch.start();
  final result = fn();
  watch.stop();
  final elapsed = '${watch.elapsedMilliseconds}ms';
  return '[${elapsed.padLeft(6)}] $result returned.';
}

void main() {
  Iterable<int> numbers = 1.to(30000000);
  final calculateSum = (() => numbers.sum()).memo;

  print(time(calculateSum));
  print(time(calculateSum)); // It returns the memoized value.

  numbers = 1.to(45000000);
  calculateSum.requestUpdate();
  print(time(calculateSum)); // Recomputed at this point.
}
11
likes
0
points
923
downloads

Publisher

verified publishersylfree.com

Weekly Downloads

Useful function wrappers for avoiding unnecessary computation. it makes the function return a memoized(cached) value.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

to_string_pretty

More

Packages that depend on memoized