-
-
Notifications
You must be signed in to change notification settings - Fork 209
Add entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries
#243
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
Conversation
|
I like the idea! I would model it after JavaScripts |
|
@lstrojny I'm not too versed in JavaScript, but from MDN:
php > $obj = (object) ['a' => 3];
php > var_dump($obj);
object(stdClass)#1 (1) {
["a"]=>
int(3)
}
php > var_dump($obj->a);
int(3)
php > var_dump((array) $obj);
array(1) {
["a"]=>
int(3)
}So I don't see the connection. I want to return an integer-keyed array, specifically |
|
Just to clarify: So this is how it could look for Functional: Functional\entries(['foo' => 'bar', 'baz' => 'bla']); // [['foo', 'bar'], ['baz', 'bla']]
Functional\from_entries([['foo', 'bar'], ['baz', 'bla']]); // ['foo' => 'bar', 'baz' => 'bla'] |
|
Ah, I see! Yes, that would be useful, but there's a subtle difference in the way I made it: Functional\enumerate(['H' => `'Hydrogen']);` // [0 => ['H' => 'Hydrogen]]Difference being that it keeps the key-value map, meaning it can be reversed with I'm not entirely sure which is more useful in general. The one I implemented was the one I wanted, but I'll defer to you as to which you prefer/think is more useful |
|
Not sure if this destructuring syntax is legal in PHP, though. foreach (enumerate($this->hash) as $i => [$k => $v]) {
// ...
}But with @lstrojny suggestion you could do: foreach (entries($this->hash) as $i => [$k, $v]) {
// ...
} |
enumerate, similar to Python'sentries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries ---Add enumerate, similar to Python's---
entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries ---Add enumerate, similar to Python's---entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries
|
@lstrojny I updated the code as per your recommendations The failing tests seem to be unrelated to my changes (I hope) |
0e8e4d9 to
fc75419
Compare
|
I've made the changes you requested, I believe. Apologies for the delay |
lstrojny
left a comment
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.
Looking very good already. Few small things, see inline. Also: could you update the docs to include it?
…tries` (and Python's `enumerate`) and `Object.from_entries`, respectively Co-authored-by Lars Strojny <lars@strojny.net> This commits adds: - Functional\entries - Functional\from_entries - InvalidArgumentException::assertPair - EntriesFromEntriesTest - Documentation for the added functions
|
I believe it's finally done :) |
|
Great, thank you for your patience! |
|
@lstrojny Has this been released? |
Previous title: Add
enumerate, similar to Python'sThis PR adds an
enumerate(Transversable|array, int=)function that acts like Python'senumerateMy use case is to iterate over an array with string keys and have an associated integer that can be used as an ID