Skip to content

feat: introduce mapEach() method #834

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

nikophil
Copy link
Member

@nikophil nikophil commented Feb 26, 2025

I'm bringing back this mapEach() method.

On the project I'm working on, we have tons of factories with "withers" which prevents to use sequences and fluent usage of the factories when collections are involved.

I'm opened to any better api, or naming 😅

@nikophil nikophil requested a review from kbond February 26, 2025 22:11
@smnandre
Copy link
Contributor

smnandre commented Mar 8, 2025

withEach ? Or maybe even eachWith ?

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding the docs for this with a real example? It'd help me understand the feature more I think.

@aegypius
Copy link

withEach ? Or maybe even eachWith ?

I agree mapEach seem to be a little redundant why not a simple map(callable callback): self like our beloved PHP\Ds Sequence::map(...)

*
* @return self<T, TFactory>
*/
public function mapEach(callable $callback, array $values): self // @phpstan-ignore missingType.callable (cannot properly type the callable)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function mapEach(callable $callback, array $values): self // @phpstan-ignore missingType.callable (cannot properly type the callable)
public function map(callable $callback, array $values): self // @phpstan-ignore missingType.callable (cannot properly type the callable)

@smnandre
Copy link
Contributor

Maybe then it would be better to use the same signature as array_map (the variadic values) ?

*
 * @return self<T, TFactory>
 */
function map(callable $callback, array... $arguments): self

Instead of calling map($callable, [[1, 2, 3], [a, b, c]]) , one could do map(callable, [1,2,3], [4,5,6])

@nikophil nikophil marked this pull request as draft April 17, 2025 05:33
@nikophil
Copy link
Member Author

yeah this could be a good idea... I still need to work on this one and think of a good real life example to convince @kbond 😆

I'm not really sure map() is a good name, the main difference is that this method effectively applies a callback on each element of a FactoryCollection, but it will actually pass a different item as a parameter of the callback on each iteration.

It's a mix between a "map()" and the new distribute() method recently introduced.

BTW, I'm still not 100% convince of the API of this method. But I really would like something to call so-called "state method" on each element of a FactoryCollection

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants