Tuesday, May 20, 2008

Added diff and intersect to the Hash object making the primes example even smaller.

/*python version*/
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]

/*common php version*/
$noprimes = xR(2,8)->mapf('xR($x*2,50,$x)->out()')->flatten();
$primes = xR(2,50)->diff($noprimes);

Or as a lovely one liner

$primes = xR(2,50)->diff(xR(2,8)->mapf('xR($x*2,50,$x)->out()')->flatten());

A little less readable for sure but the reality is it can be done and it fits the true definition of "one liner" by sliding in at the 79th column.

No comments: