-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Conversation
Hey @Aetet @brianegan, is this repo alive or something? I can make a fork/re-make of this myself... |
Hey. |
lib/memoize.dart
Outdated
A prevArg; | ||
R prevResult; | ||
bool isInitial = true; | ||
HashMap<A, R> argsToOutput = HashMap(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better refer to the documentation https://api.dart.dev/stable/2.12.2/dart-collection/HashMap/HashMap.html :)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:)
@TheLastGimbus a gentle ping :) |
# Conflicts: # lib/memoize.dart
...I hope
I hope ;_;
HIiiiiii!I know it took some time, but here it is! I updated branch with I also fixed (I hope that's the right word 😅) test - they were designed that function re-executes every time something changes - please take a look at 84c2854 @sinegovsky-ivan I don't have any good idea for limiting the capacity tho 😕 It's a Func1<A, R> memo1<A, R>(Func1<A, R> func, {int? maxSize}) {
final argsToOutput = HashMap<List, R>(
equals: (a, b) => _listEquals(a, b),
hashCode: (l) => _listHashCode(l),
);
// External list to keep track of cache order
final hashes = <int>[];
return ((A aA) {
final cached = argsToOutput[[aA]];
if (cached != null) {
return cached;
} else {
final res = func(aA);
argsToOutput[[aA]] = res;
// Skip the whole thing if user doesn't use it
if (maxSize != null) {
hashes.add(_listHashCode([aA]));
if (argsToOutput.length > maxSize) {
// Pop the first (oldest) one
// I'm not sure how efficient this is :/
argsToOutput.removeWhere((k, _) => _listHashCode(k) == hashes[0]);
hashes.removeAt(0);
}
}
return res;
}
});
} Something like this? Edit: I actually wrote simple test for the above: test('should remove old cache when maxSize set', () {
var count = 0;
var func = memo1((int a) => ++count, maxSize: 2);
expect(func(1), 1);
expect(func(1), 1);
expect(func(2), 2);
expect(func(2), 2);
expect(func(1), 1);
expect(func(3), 3);
// At this point, first cache element should be removed
expect(func(1), 4);
}); and it passes! ...oh, and also - I didn't know what to do with |
@TheLastGimbus hey! I come to you with many thoughts about this PR:
|
Great project and PR! Thanks for your review @missvalentinep. Thanks @TheLastGimbus for your PR, would love to see this merged. |
Hi there!
I was looking for something like
@functools.lru_cache
from Python in Dart, and came across this library. But it's currently remebering only one result - kinda useless in my opinion 😕So I'm proposing a solution to write down results with a
HashMap
Currently I added only
memo1
, but if you like it, I will extend it to all the others - probably nestedHashMap<A, HashMap<...>>
will be used for thatI also added an example code to demonstrate how it makes things way faster 🚀
Closes #10