|
Project Information
Members
Links
|
The goal of this project is to bring a nicer syntax for dealing with arrays to php itself. immediate it just wraps a lot of the existing array functions into an object which allows a more object oriented way of handling data. Immediately this project is prototyped in php itself but eventually I would like to provide a c php extension. To provide a quick example here are some things possible with this project in comparison to other languages: //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]
//phparrayplus version:
$noprimes = xR(2,8)->for_each('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)->for_each('xR($x*2,50,$x)->out')->flatten);
//common lisp:
(loop for x from 0 to 100 if (> (* x x) 3) collect (* 2 x))
//phparrayplus version:
$C = xR(100)->if_only('($x*$x) > 3')->for_each('2*$x');
//factorial in phparrayplus, nothing else to compare this too - just cool:
function fac($n){
return xR(1,$n)->reduce('$x*$y');
}
//fib sequence in phparrayplus
php -e xH(0,1)->expand('$x+$y',20)
//perl
perl -e'@p=(0,1);until($#p>20){print"$p[-2]\nā;push @p,$p[-2]+$p[-1]}ā
//ruby
ruby -e āp,c=0,1;20.times{p p;c=p+p=c}ā
//python version:
S = [x**2 for x in range(10)]
V = [2**i for i in range(13)]
M = [x for x in S if x % 2 == 0]
P = [str(round(355/113.0, i)) for i in range(1,6)]
F = [x.strip() for x in [' banana', ' loganberry ']]
VEC = [2, 4, 6]
X = [3*x for x in vec if x > 3]
X1 = [[x,x**2] for x in vec]
//phparrayplus version:
$S = xR(10)->for_each('pow($x,2)');
$V = xR(13)->for_each('pow($x,2)');
$M = xH($S)->if_only('$x % 2 == 0');
$P = xR(1,6)->for_each('round(355/113.0,$x)');
$F = xH(' banana', ' loganberry ')->map('trim');
$VEC = xH(2,4,6);
$X = xH($VEC)->if_only('$x > 3')->for_each('3*$x');
$X1 = xH($VEC)->for_each('Array($x,pow($x,2))');It may not be as pretty as python (mainly in that code blocks have to be passed as strings) but I think the syntax is fairly readable/sensible. I will be updating the wiki shortly to reflect the entire api as well as uploading the initial release of the codebase (all one files). |