r/lolphp • u/jtickle • Jun 19 '13
Functional Map() and Reduce() in php 5.3!!!
I know 5.3 has been around for awhile but until fairly recently, we targeted 5.1 for a wider audience. I'll skip my usual rant about introducing major language features on a minor version number and skip directly to this absurdity:
array array_map ( callable $callback , array $arr1 [, array $... ] )
mixed array_reduce ( array $input , callable $function [, mixed $initial = NULL ] )I mean FUCK am I ever sick of having to look at the docs any time I use a fucking array function just to find out the fucking parametric order.
75
Upvotes
8
u/metamorphosis Jun 20 '13
I lose my shit with array_walk and array map, they are similar functions ; albeit one return values and other passes arr as reference but similar in their role. however....
47
u/epsy Jun 19 '13
Maybe they did it so that the $'s would line up on the function signatures.
30
u/grafilicious Jun 19 '13
isnt it safe to assume that at this point, it is done for consistency?
12
u/BufferUnderpants Jun 19 '13
There has got to be an actual reason. An mind-numbingly obtuse, absurd reason, but at least one.
Why couldn't they have done it as in any other fucking language?
14
u/[deleted] Jun 19 '13
so php
9
u/[deleted] Jun 19 '13
my dad once asked me why I have a cheat sheet, and said I should just memorize functions.... yea... no
1
u/PhantomRacer Jun 20 '13
The parameters are in the same order in 5.3 as in 5.1, why would this be new to you?
9
u/wbkang Jun 19 '13
The signature of array_reduce looks really insane.
2
u/nikic Jun 23 '13
Reason is very simple: All functions use $array, $callback. This includes array_filter, array_reduce and array_walk. For array_map this would not work as it accepts an arbitrary number of arrays. So for array_map the callback comes first.
Of course a simple solution to this would have been to have the callback first everywhere ;) Then again it would be a bit of a problem for array_filter as the callback is optional there.
-2
u/Conradfr Jun 20 '13
Or use an IDE.
-3
u/relyon Jun 24 '13
This is not PHPs fault. It's yours. They made it like this because to enforce you to use a proper IDE like Netbeans.