Skip to content
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

Adding enumerate #43

Closed
wants to merge 1 commit into from
Closed

Adding enumerate #43

wants to merge 1 commit into from

Conversation

ragboyjr
Copy link
Contributor

Adding ability to enumerate over key-value tuples
of iterators

Signed-off-by: RJ Garcia rj@bighead.net

@ragboyjr
Copy link
Contributor Author

@nikic any word?

@nikic
Copy link
Owner

nikic commented Jun 28, 2017

Do you have any specific use case in mind for this function? I realize that this is a "standard" function, but I'm not sure if it's useful in a language that has a native concept of key-value pairs in iterators (the lack of which enumerate() works around in other languages).

@ragboyjr
Copy link
Contributor Author

ragboyjr commented Jun 28, 2017

@nikic yes, I need to use it all the time when I need to map a set of items and use the key.

The issue is that iter\map doesn't send the key parameter in the mapping function.

For example:

$rows_from_db = $db->query();
$html_items = iter\map(function($tup) {
  list($index, $row) = $tup;
  return sprintf("<li class="%s">%s</li>", $index % 2 ? 'odd' : 'even', $row['name']);
}, iter\enumerate($rows_from_db));

The only other way to accomplish this through the library would be to use reduce the database rows and concatenate.

@nikic
Copy link
Owner

nikic commented Jun 28, 2017

@ragboyjr That makes sense, thanks. Could you please add another test that shows enumerate() behavior on an array with (string) keys?

Adding ability to enumerate over key-value tuples
of iterators

Signed-off-by: RJ Garcia <rj@bighead.net>
@ragboyjr
Copy link
Contributor Author

ragboyjr commented Jun 28, 2017

@nikic sure, like that?

@nikic
Copy link
Owner

nikic commented Jun 28, 2017

Merged as 95d877c. Thanks!

@nikic nikic closed this Jun 28, 2017
@ragboyjr
Copy link
Contributor Author

Awesome thanks!

@ragboyjr ragboyjr deleted the enumerate branch June 28, 2017 20:15
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.

None yet

2 participants