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

Add mapWithKeys() #81

Merged
merged 2 commits into from
Sep 12, 2020
Merged

Add mapWithKeys() #81

merged 2 commits into from
Sep 12, 2020

Conversation

judahnator
Copy link
Contributor

#64 caught my eye. I noticed that changing how the existing methods worked would cause issues, so I made a new mapWithKeys() method that passes the iterator key in addition to its value to the callback.

Here is a copy/paste of the test I wrote for it, to see it in action:

$mapped = mapWithKeys(func\operator('*'), range(0, 5));
$this->assertSame([0, 1, 4, 9, 16, 25], toArray($mapped));

$mapped = mapWithKeys(
    function ($v, $k) { return sprintf('%s%s', $k, $v); },
    ['foo' => 'bar', 'bing' => 'baz']
);
$this->assertSame(
    ['foo' => 'foobar', 'bing' => 'bingbaz'],
    toArrayWithKeys($mapped)
);

@SamMousa
Copy link
Contributor

LGTM, would definitely use this if it gets merged...

@nikic
Copy link
Owner

nikic commented Sep 11, 2020

Looks reasonable to me as well. I think it needs to be added to the rewindable namespace as well.

@SamMousa
Copy link
Contributor

Since this PR is pretty old, what about merging this one and either: creating an issue for the rewindable version, or implementing that ourselves?

@judahnator
Copy link
Contributor Author

Oh hey! I had given up hope on this PR, glad to see it is not quite dead yet.

I looked at how the rewindable stuff was laid out and did my best to copy the implementation and test for it. Let me know if anything is amiss!

@nikic nikic merged commit 170f93c into nikic:master Sep 12, 2020
@nikic
Copy link
Owner

nikic commented Sep 12, 2020

Thanks!

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.

3 participants