Friday, May 16, 2008

Prime numbers in PHP

More fluent interface goodness.

noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
primes = [x for x in range(2, 50) if x not in noprimes]

$noprimes = xR(2,8)->for_each('xR($x*2,50,$x)->out()')->flatten();
$primes = xR(2,50)->if_only('!xH('.$noprimes->implode().')->in($x)');

Edited above to reflect the fact I have aliased filterf with if_only (I would love to use if but it is clearly reserved) and mapf with for_each (again would love to use just for, each foreach etc.). Still I feel it reads a lot cleaner.

No comments: