4 ms·
One use case for this would be unstacking a bunch of array_* functions since the nesting can start getting to be a little dense. Here's a common function I run
by Minor49er 5y ago
One use case for this would be unstacking a bunch of array_* functions since the nesting can start getting to be a little dense. Here's a common function I run into a lot:
function cleanIdsTraditionally(array $input): array
{
return array_unique(array_filter(array_map('intval', $input)));
}
Here it is as the piped version:
function cleanIdsWithPipe(array $input): array
{
return pipe($input,
fn($input) => array_map('intval', $input),
fn($input) => array_filter($input),
fn($input) => array_unique($input)
);
}
Here, I think the added context of the pipe negates the enhanced readability of having each method on its own line. Anything more complex than this would benefit from being piped, but at that point, it would probably be better to break the steps down into more discrete functions anyways.
Overall, not a bad idea, but in practice, it would be hard to justify having around
- sandreas 5y agoWhile that is correct, I would definetely prefer using the syntax used in Symfony ArrayCollection[1]: $collection = new ArrayCollection([4,3,2,1]); $collection->filter(fn($element) => $element % 2 == 0) ->map(fn($element) => $element+2) ->toArray(); It is pretty much like in JavaScript or C# with Linq, but with less features. But you can implement this yourself using - ArrayInterface, Traversable, Countable, etc. - generators / iterators (yield) It's not that hard. There is also YaLinqo[2] [1] https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html https://www.doctrine-project.org/projects/doctrine-collectio... [2] https://github.com/Athari/YaLinqo https://github.com/Athari/YaLinqo
- Minor49er 5y agoVery useful for more complex situations. Thanks for this