Kamis, 23 September 2010

Perl vs PHP (a bit of credit to PHP)

Just read this blog post. Comments are disabled, so I thought I'd add a blog post.

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

2 komentar:

  1. which makes it 13 characters shorter than his Perl example :)

    BalasHapus
  2. I'm no golfer but this is a bit shorter…

    ($longest)=sort{length$b<=>length$a}split$/,$str;

    BalasHapus

Catatan: Hanya anggota dari blog ini yang dapat mengirim komentar.