foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat: introduce `mapEach()` method

Open nikophil opened this issue 1 year ago • 4 comments

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 :sweat_smile:

nikophil avatar Feb 26 '25 21:02 nikophil

withEach ? Or maybe even eachWith ?

smnandre avatar Mar 08 '25 23:03 smnandre

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(...)

aegypius avatar Mar 18 '25 13:03 aegypius

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])

smnandre avatar Apr 17 '25 03:04 smnandre

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 :laughing:

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

nikophil avatar Apr 17 '25 05:04 nikophil