-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add tables.mgetOrPutLazy #52
Open
zah
wants to merge
1
commit into
master
Choose a base branch
from
mgetOrPutLazy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,15 @@ template init*[A, B](T: type OrderedTableRef[A, B]): auto = newOrderedTable[A, B | |
template init*[A](T: type CountTable[A]): auto = initCountTable[A]() | ||
template init*[A](T: type CountTableRef[A]): auto = newCountTable[A]() | ||
|
||
template mgetOrPutLazy*[A, B](t: Table[A, B], key: A, val: B): var B = | ||
type R = B | ||
|
||
proc setter(loc: var R): var R = | ||
if loc == default(R): | ||
loc = val | ||
loc | ||
|
||
setter(mgetOrPut(t, key, default(R))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks pretty odd - ie if |
||
|
||
export tables, objects | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import | ||
unittest, | ||
../stew/shims/tables | ||
|
||
suite "tables": | ||
test "mgetOrPutLazy": | ||
var t = {"a": 10, "b": 20}.toTable | ||
check t.mgetOrPutLazy("a", 30) == 10 | ||
check t.mgetOrPutLazy("c", 40) == 40 | ||
|
||
check t.mgetOrPutLazy("c", 20) == 40 | ||
|
||
t.mgetOrPutLazy("c", 20) = 15 | ||
check t.mgetOrPutLazy("c", 40) == 15 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
so if the collection had a value that was all zeroes (as opposed to not having a value), this will update it to
val
which doesn't seem to be the intentThere 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, this version of code comes with a number of caveats, but the idea of having a shim is that it allows us to capture the intention of the user until a more general and efficient version of the API is added to the
tables
module (as a template similar towithValue
).At the moment it's reasonable to use mostly with tables with
ref
values that don't need to storenil
. Thedefault
function for the type should be cheap and not a valid value in the usage context.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.
well, the that's the point a bit - my intent is not to set if the value is the default - it's to set / create only if it's not in the collection already