Skip to content

Add entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries - #243

Merged
lstrojny merged 1 commit into
lstrojny:mainfrom
someonewithpc:main
Nov 29, 2021
Merged

lstrojny merged 1 commit into
lstrojny:mainfrom
someonewithpc:main

Conversation

@someonewithpc

@someonewithpc someonewithpc commented Nov 2, 2021 •

Copy link
Copy Markdown
Contributor

Previous title: Add enumerate, similar to Python's

This PR adds an enumerate(Transversable|array, int=) function that acts like Python's enumerate

foreach (enumerate($this->hash) as $i => [$k => $v]) {
    // ...
}

My use case is to iterate over an array with string keys and have an associated integer that can be used as an ID

@lstrojny

lstrojny commented Nov 2, 2021

Copy link
Copy Markdown
Owner

I like the idea! I would model it after JavaScripts Object.fromEntries() and Object.entries() function and have both functions available.

@someonewithpc

someonewithpc commented Nov 2, 2021 •

Copy link
Copy Markdown
Contributor Author

@lstrojny I'm not too versed in JavaScript, but from MDN:

Return:
An array of the given object's own enumerable string-keyed property [key, value] pairs

Object.entries can already be achieved by casting an array to an (object) (and the converse with a cast to (array)):

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
These may also be useful, for sure, but unless I'm not understanding it right, it'd be a completely different pair of functions

@lstrojny

lstrojny commented Nov 2, 2021 •

Copy link
Copy Markdown
Owner

Just to clarify: Object.entries() does exactly what your (and Python's) enumerate() does and Object.fromEntries() does the opposite.

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']

@someonewithpc

Copy link
Copy Markdown
Contributor Author

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 array_merge(...$enumerated).

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

@jdreesen

jdreesen commented Nov 2, 2021 •

Copy link
Copy Markdown

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]) {
    // ...
}

@someonewithpc someonewithpc changed the title Add enumerate, similar to Python's Add entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries ---Add enumerate, similar to Python's--- Nov 3, 2021
@someonewithpc someonewithpc changed the title Add entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries ---Add enumerate, similar to Python's--- Add entries and from_entries, similar to JavaScript's Object.entries and Object.fromEntries Nov 3, 2021
@someonewithpc

Copy link
Copy Markdown
Contributor Author

@lstrojny I updated the code as per your recommendations

The failing tests seem to be unrelated to my changes (I hope)

Comment thread src/Functional/Entries.php
Comment thread src/Functional/Entries.php Outdated
Comment thread src/Functional/FromEntries.php Outdated
Comment thread src/Functional/FromEntries.php Outdated
@someonewithpc
someonewithpc force-pushed the main branch 2 times, most recently from 0e8e4d9 to fc75419 Compare November 12, 2021 11:09
@someonewithpc

Copy link
Copy Markdown
Contributor Author

I've made the changes you requested, I believe. Apologies for the delay

@lstrojny lstrojny left a comment

Copy link
Copy Markdown
Owner

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?

Comment thread src/Functional/FromEntries.php
Comment thread src/Functional/FromEntries.php Outdated
Comment thread src/Functional/Entries.php
…tries` (and Python's `enumerate`) and `Object.from_entries`, respectively

Co-authored-by Lars Strojny <[email protected]>

This commits adds:

 - Functional\entries
 - Functional\from_entries
 - InvalidArgumentException::assertPair
 - EntriesFromEntriesTest
 - Documentation for the added functions
@someonewithpc

Copy link
Copy Markdown
Contributor Author

I believe it's finally done :)

@lstrojny

Copy link
Copy Markdown
Owner

Great, thank you for your patience!

@lstrojny
lstrojny merged commit 5f5ddb6 into lstrojny:main Nov 29, 2021
@lol768

lol768 commented Apr 5, 2022

Copy link
Copy Markdown

@lstrojny Has this been released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants