string functions use (haystack, needle) and array functions use (needle, haystack)
because that's the way the underlying C libraries also worked
string functions use (haystack, needle) and array functions use (needle, haystack)
because that's the way the underlying C libraries also worked
array_filter takes (arr, callback)
https://www.php.net/manual/en/function.array-filter.php
array_map takes (callback, arr)
One function works against a single element, whereas the other works against multiple. In that case, the parameter order is more meaningful. You can use array_walk if you want (arr, callback), but that only works against a single array -- similarly to array_filter.
Array filter is "filter this array with this function".
Array map is "map this function over this array".
But I agree any replacement function should be consistent with Haskell.
"Filter for this function in this array"
"Map over this array with this function"
When you use the correct verbiage, the parameter order makes sense.
One filters something with something else, in the real world. Filter water with a mesh etc.
And (in maths, at least) one maps something onto something else. (And less commonly one maps an area onto paper etc.)
Just because you can make your two sentences does not make them natural word order.
Yes, but that's the opposite of what you said earlier. You might map x onto 2*x, for example. Or, if you're talking about a collection, you might map the integers 0..10 on to double their value. Data first, then the way you're manipulating it. I'm a mathematician and this is what makes sense to me.
I would only say "map this function..." if the function itself is being manipulated somehow (mapped onto some other value).
There’s enough viral videos online of how even neighbouring European counties order common sentences differently. Even little things like reading the time (half past the previous hour vs half to the next hour) and counting is written differently in different languages.
So modelling the order of parameters based on English vernacular doesn’t make a whole lot of sense for programming languages used by programmers of all nationalities.
Well that’s good, because I didn’t.
I feel like this is a weak defence of the internally inconsistent behaviour. As someone who has been programming with PHP for over twenty years now, most of them professionally, I still cannot remember the needle/haystack order in these functions, I thank intellisense for keeping me sane here.
As evident with this pipe operator, or with for example Attributes, PHP does not need to religiously follow the C way of doing things, so why not improve it instead of dismissing it as "it is the way it is because that is the way it was"?
There isn't a good reason for PHP to have inherited C's issues here.
We are not in the early days though, and in many other aspects PHP evolved greatly.
If we want to change the param order of str/array functions for php, I think we should start with fixing the C libraries. That seems like a better starting point. The impact will certainly be more beneficial to even more developers than just php.
The fact that they are chaotic since 30 years ago is not a valid reason for keeping them chaotic right now.
Also, I'm not even arguing they should change the existing functions, that would break all existing code for almost no reason.
I think they should "simply" support methods on primitives, and implement the main ones in a chainable way:
"test string"->trim()->upper()->limit(100);
[0,1,2]->filter(fn ($n) => $n % 2 === 0)->map(fn($n) => $n * 2);
I would love this so much
How it that consistent?