Skip to content

Conversation

@someonewithpc
Copy link
Contributor

@someonewithpc someonewithpc commented Nov 2, 2021

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
Copy link
Owner

lstrojny commented Nov 2, 2021

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

@someonewithpc
Copy link
Contributor Author

someonewithpc commented Nov 2, 2021

@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
Copy link
Owner

lstrojny commented Nov 2, 2021

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
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
Copy link

jdreesen commented Nov 2, 2021

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
Contributor Author

@lstrojny I updated the code as per your recommendations

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

@someonewithpc someonewithpc force-pushed the main branch 2 times, most recently from 0e8e4d9 to fc75419 Compare November 12, 2021 11:09
@someonewithpc
Copy link
Contributor Author

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

Copy link
Owner

@lstrojny lstrojny left a 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
@someonewithpc
Copy link
Contributor Author

I believe it's finally done :)

@lstrojny
Copy link
Owner

Great, thank you for your patience!

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

lol768 commented Apr 5, 2022

@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