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

unary(<callable>) #11

Open
meszaros-lajos-gyorgy opened this issue Apr 21, 2021 · 0 comments
Open

unary(<callable>) #11

meszaros-lajos-gyorgy opened this issue Apr 21, 2021 · 0 comments
Labels
enhancement New feature or request need more info Further information is requested, or there are still questions on how to solve the issue

Comments

@meszaros-lajos-gyorgy
Copy link
Owner

unary will be needed, if map/filter/etc will also add the index and collection parameters for the callbacks.

current code, which will call intval for every member of the array:

A::map('intval', ['1', '2', '3', '4']); // [1, 2, 3, 4]

is equivalent to:

A::map(function($value) {
  return intval($value);
}, ['1', '2', '3', '4']);

After #9 the calls will be like:

A::map('intval', ['1', '2', '3', '4']); // [1, 0, 0, 0]

which is equivalent to:

A::map(function($value, $index, $collection) {
  return intval($value, $index, $collection);
}, ['1', '2', '3', '4']);

// intval('1', 0); -> 1
// intval('2', 1); -> 0
// intval('3', 2); -> 0
// intval('4', 3); -> 0

The solution would be:

A::map(unary('intval'), ['1', '2', '3', '4']);

which is like:

A::map(function($value, $index, $collection) {
  return intval($value);
}, ['1', '2', '3', '4']);
@meszaros-lajos-gyorgy meszaros-lajos-gyorgy added enhancement New feature or request need more info Further information is requested, or there are still questions on how to solve the issue labels Apr 22, 2021
@meszaros-lajos-gyorgy meszaros-lajos-gyorgy changed the title unary() unary(<callable>) Apr 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request need more info Further information is requested, or there are still questions on how to solve the issue
Projects
None yet
Development

No branches or pull requests

1 participant