There are endless ways we can sneer at PHP's deficiencies, but since 5.3 PHP already supports anonymous subroutines, via the function (args) { ... } syntax. So:
$longestLine = max(
array_map(
create_function('$a', 'return strlen($a);'),
explode("\n", $str)
)
);
can be rewritten as:
$longestLine = max(
array_map(
function($a) { return strlen($a); },
explode("\n", $str)
)
);
though the example is not a good one since it might as well be:
$longestLine = max(
array_map(
'strlen',
explode("\n", $str)
)
);
which makes it 13 characters shorter than his Perl example :)
BalasHapusI'm no golfer but this is a bit shorter…
BalasHapus($longest)=sort{length$b<=>length$a}split$/,$str;