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

Array keys in callable for 'reduce' function #27

Closed
alxsad opened this issue Aug 21, 2015 · 5 comments
Closed

Array keys in callable for 'reduce' function #27

alxsad opened this issue Aug 21, 2015 · 5 comments

Comments

@alxsad
Copy link

alxsad commented Aug 21, 2015

I think will be better if developers will be able to get a key.

Example:

use iter as f;

f\reduce(function ($acc, $value, $key) {
    $acc .= sprintf("%s: %s\n", $key, $value);
    return $acc;
}, $response->headers->all(), '');

Or I can do it otherwise?

@3onyc
Copy link

3onyc commented Aug 24, 2015

Having $key as the last parameter would be similar to array_walk, how to handle $startValue though?

@alxsad
Copy link
Author

alxsad commented Aug 24, 2015

$key as the last parameter in callable.

function reduce(callable $function, $iterable, $startValue = null) {
    _assertIterable($iterable, 'Second argument');
    $acc = $startValue;
    foreach ($iterable as $key => $value) {
        $acc = $function($acc, $value, $key);
    }
    return $acc;
}

@3onyc
Copy link

3onyc commented Aug 24, 2015

Wasn't fully awake yet, problem I raised doesn't exist.

@alxsad You could make a pull request adding the key parameter + tests.

@alxsad
Copy link
Author

alxsad commented Aug 24, 2015

@3onyc #28

@nikic
Copy link
Owner

nikic commented Aug 25, 2015

#28 is merged.

@nikic nikic closed this as completed Aug 25, 2015
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

No branches or pull requests

3 participants