-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
stdlib 0.40: change the implementation of dict.update
#659
base: main
Are you sure you want to change the base?
Conversation
dict.update
src/gleam/dict.gleam
Outdated
|
||
/// A dictionary of keys and values. | ||
/// A dictionary (dict) of keys and values. |
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.
Should maybe "dict" in comments be replaced by Dict
?
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.
Revert this please 🙏
CHANGELOG.md
Outdated
@@ -1,5 +1,11 @@ | |||
# Changelog | |||
|
|||
## v0.40.0 - Unreleased | |||
|
|||
- The previously deprecated `update` function of the `dict` has been replaced |
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.
I am not fond of this changelog entry.
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.
I think we can just say "The dict
module gains the update
function.".
src/gleam/dict.gleam
Outdated
@@ -474,13 +474,63 @@ pub fn upsert( | |||
|> insert(dict, key, _) | |||
} | |||
|
|||
@deprecated("This function has been renamed to `upsert`") | |||
/// Returns a dict where the caller attempts to change a key-value pair in the |
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.
I am not happy with this explanation.
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.
Any better ideas?
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.
Perhaps something more concise?
Updates the value in a dict for a given key using a given function.
If there already was a value in the dict for the key then it is passed into the function.
If a value is returned from the function then it will be used as the new value in the dict. If no value is returned then any previous value is removed.
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.
If a value is returned from the function then it will be used as the new
value in the dict. If no value is returned then any previous value is removed.
But is this true?
The change callback can on None case add the key by returning Ok(value) (or Some(value))? Or did I miss something.
I am updating the tests so it is clearer that the change function is very powerful and flexible.
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.
This shows an example where I cannot see that the function description above matches. Because it really depends on the change callback/function what happens here.
I think this function should be called change. I think the other implementation already done should be called update https://github.com/gleam-lang/stdlib/pull/610/files. Update means that there already is something and it can fail to update. Update does not signal removal. Change signals removal. Change fits to this PR, update to the other. |
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.
Thank you
src/gleam/dict.gleam
Outdated
pub fn update( | ||
in dict: Dict(k, v), | ||
update key: k, | ||
with fun: fn(Option(v)) -> Result(v, Nil), |
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.
Could you use Option
for the return value here please
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.
I thought we are not using Option for Return values.
If we have regular functions following that pattern, we then cannot apply those in this update function?
CHANGELOG.md
Outdated
@@ -1,5 +1,11 @@ | |||
# Changelog | |||
|
|||
## v0.40.0 - Unreleased | |||
|
|||
- The previously deprecated `update` function of the `dict` has been replaced |
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.
I think we can just say "The dict
module gains the update
function.".
src/gleam/dict.gleam
Outdated
|
||
/// A dictionary of keys and values. | ||
/// A dictionary (dict) of keys and values. |
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.
Revert this please 🙏
src/gleam/dict.gleam
Outdated
@@ -474,13 +474,63 @@ pub fn upsert( | |||
|> insert(dict, key, _) | |||
} | |||
|
|||
@deprecated("This function has been renamed to `upsert`") | |||
/// Returns a dict where the caller attempts to change a key-value pair in the |
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.
Perhaps something more concise?
Updates the value in a dict for a given key using a given function.
If there already was a value in the dict for the key then it is passed into the function.
If a value is returned from the function then it will be used as the new value in the dict. If no value is returned then any previous value is removed.
/// | ||
/// dict | ||
/// |> dict.update("e", inc_if_exists_or_discard) | ||
/// // -> from_list([#("a", 0), #("b", 1), #("c", 2)]) |
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.
I don't think this shows the functionality very clearly. One example per code block would be good
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.
I am not certain I follow. Per which code block?
@lpil are you certain about this being named update rather than change? |
What's the motivation for it being change. I thought we did the whole deprecation cycle to free up the name update? |
|
test/gleam/dict_test.gleam
Outdated
// Utilize a function returning a `Result` in the change function. | ||
let half_or_zero_else_noop = fn(x) { | ||
case x { | ||
Some(i) -> int.divide(i, 2) |
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.
Because other functions usually return Result
rather than Option
, I have opted for the change function to return Result
not Option
as well.
I have added a unit test example to show what I mean.
Are you certain you want to return Option
instead?
ref #659 (comment) cc @lpil
After feedback I made the changes to return Option rather than Result and tried to simplify the docs and the unit tests |
This is still ready to be reviewed/merged unless you want me to rename this to change and have update do the other impl (see above). Please let me know if there are still things to do. |
Any chance to get this into 0.42 with either name |
Replaces #610