Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple inputs & results #12

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Files and directories created by pub
.dart_tool/
.packages

# Conventional directory for build outputs
build/

# Directory created by dartdoc
doc/api/
1 change: 1 addition & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A simple example showcase of memoize library
32 changes: 32 additions & 0 deletions example/bin/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:math' as math;

import 'package:memoize/memoize.dart';

num heavyCalc(num number) {
num sum = 0;
for (int x = 0; x < 999999; x++) {
sum += math.sqrt(number);
}
return sum;
}

void main(List<String> arguments) {
print('Some heavy calculations done 99 times with 6 different inputs:');
List<double> numbers = [10000, 4206969, 777, 21372137, 99999999, 10000];
var w = Stopwatch()..start();
for (int x = 0; x < 99; x++) {
for (var n in numbers) {
heavyCalc(n);
}
}
print('\tNo cache: ${w.elapsedMilliseconds}ms');

w.reset();
var cacheSqrt = memo1(heavyCalc);
for (int x = 0; x < 99; x++) {
for (var n in numbers) {
cacheSqrt(n);
}
}
print('\tWith cache: ${w.elapsedMilliseconds}ms');
}
9 changes: 9 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: example
description: Example showcase of memoize library

environment:
sdk: '>=2.0.0-dev <3.0.0'

dependencies:
memoize:
path: ../
17 changes: 7 additions & 10 deletions lib/memoize.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:collection';

import 'function_defs.dart';

/// Lazy evaluates function and returns cached result on each call.
Expand All @@ -20,19 +22,14 @@ Func0<R> memo0<R>(Func0<R> func) {
/// Checks 1 argument for equality with [==] operator and returns the cached
/// result if it was not changed.
Func1<A, R> memo1<A, R>(Func1<A, R> func) {
A prevArg;
R prevResult;
bool isInitial = true;
HashMap<A, R> argsToOutput = HashMap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about limit storage capacity? For example second argument (with default value) to memo1.

Otherwise, in some cases, we will waste a lot of memory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you mean to limit maximum cache? Yeah, that would be useful

Tho i'm not sure how to do this - if you just pop() one item from a HashMap in dart, does it do it in some order or just random?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so generally - you like/want my change in general sense? If so, I can implement rest of the features - just wasn't sure if I even should

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I like your changes:)


return ((A arg) {
if (!isInitial && arg == prevArg) {
return prevResult;
if (argsToOutput.containsKey(arg)) {
return argsToOutput[arg];
} else {
prevArg = arg;
prevResult = func(arg);
isInitial = false;

return prevResult;
argsToOutput[arg] = func(arg);
return argsToOutput[arg];
}
});
}
Expand Down