-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 yield key
in Hash#transform_values
and value
in #transform_keys
#13608
Add yield key
in Hash#transform_values
and value
in #transform_keys
#13608
Conversation
…value in Hash#transform_keys
spec/std/hash_spec.cr
Outdated
expected_values = h1.values | ||
h2 = h1.transform_keys do |x, v| | ||
v.should eq(expected_values.shift) | ||
x + 1 | ||
end | ||
h2.should eq({2 => "a", 3 => "b", 4 => "c"}) |
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.
Food for thought: Maybe a simpler implementation could combine key and value?
expected_values = h1.values | |
h2 = h1.transform_keys do |x, v| | |
v.should eq(expected_values.shift) | |
x + 1 | |
end | |
h2.should eq({2 => "a", 3 => "b", 4 => "c"}) | |
h1.transform_keys{ |k, v| "#{k}#{v}" }.should eq({"2a" => "a", "3b" => "b", "4c" => "c"}) |
The same can be applied to all other tests.
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.
updated.
Note: for transform_key!
I had to change the type of the hash from Hash(String, Int32)
to Hash(String, String)
since you can't change the value type in place so that it could use the key value in that spec.
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.
Thanks for the contribution @baseballlover723! Let me add to what was said that the types of the blocks are not respecting the change, and it would be great if the docs can reflect the change too with a little comment.
Let us know if you want us to take over, or if you prefer to take it to completion.
@beta-ziliani I'd prefer to take it to completion if possible. To clarify, is what you're asking me to do to update the function defs from I'll also update the docs as well, though I don't really want to change the example code structure, so the added value will just be unused in the example block. |
yes, exactly, that's what I mean. About the documentation, I just meant to add a line stating that the value/key are also yielded to the block. thanks for taking it! |
@beta-ziliani updated the docs, let me know if the wording is ok with you. |
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.
Awesome, thanks @baseballlover723 !
key
in Hash#transform_values
, #transform_values!
and value
in #transform_keys
key
in Hash#transform_values
, #transform_values!
and value
in #transform_keys
key
in Hash#transform_values
and value
in #transform_keys
Fixes #13581