scratch

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 86  →  ?path2? @ 87
/vendor/fusonic/linq/examples/1-average.php
@@ -0,0 +1,28 @@
<?php
 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
 
use Fusonic\Linq\Linq;
$files = glob("/tmp/*");
 
// Calculate the average filesize of all files in a directory.
 
### Plain PHP: ###
 
$sum = 0;
$i = 0;
foreach($files as $file) {
$sum += filesize($file);
$i++;
}
$avg = $i == 0 ? 0 : $sum / $i;
 
echo "Average: " . $avg;
 
### Linq: ###
 
$avgL = Linq::from($files)
->select(function($f) { return filesize($f); })
->average();
 
echo "<br/><br>Average Linq: " . $avgL;
/vendor/fusonic/linq/examples/2-filter.php
@@ -0,0 +1,32 @@
<?php
 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
 
use Fusonic\Linq\Linq;
$files = glob("/tmp/*");
 
// Calculate the average filesize of all files greater than 1024 bytes in a directory.
 
### Plain PHP: ###
 
$sum = 0;
$i = 0;
foreach($files as $file) {
$currentSize = filesize($file);
if($currentSize > 1024) {
$sum += $currentSize;
$i++;
}
}
$avg = $sum / $i;
 
echo "Average: " . $avg;
 
### Linq: ###
 
$avgL = Linq::from($files)
->select(function($f) { return filesize($f); })
->where(function($fs) { return $fs > 1024; })
->average();
 
echo "<br/><br>Average Linq: " . $avgL;
/vendor/fusonic/linq/examples/3-skiptake.php
@@ -0,0 +1,44 @@
<?php
 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
 
use Fusonic\Linq\Linq;
$files = glob("/tmp/*");
 
// Calculate the average filesize of all files greater than 1024 bytes in a directory
// but skip the very first 2 files and then take only 4 files.
 
### Plain PHP: ###
 
$sum = 0;
$i = 0;
$y = 0;
foreach($files as $file) {
$currentSize = filesize($file);
if($currentSize > 1024) {
if($y < 2) {
$y++;
continue;
}
else if ($y > 5) {
break;
}
$y++;
$sum += $currentSize;
$i++;
}
}
$avg = $sum / $i;
 
echo "Average: " . $avg;
 
### Linq: ###
 
$avgL = Linq::from($files)
->select(function($x) { return filesize($x); })
->where(function($x) { return $x > 1024; })
->skip(2)
->take(4)
->average();
 
echo "<br/><br>Average Linq: " . $avgL;
/vendor/fusonic/linq/examples/4-order.php
@@ -0,0 +1,38 @@
<?php
 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
 
use Fusonic\Linq\Linq;
$files = glob("/tmp/*");
 
// Sort all files in a directory by filsize in descending order
 
### Plain PHP: ###
$data = array();
foreach($files as $file) {
$currentSize = filesize($file);
$data[] = array("name" => $file, "size" => $currentSize);
}
 
uasort($data, function($a, $b) {
$as = $a['size'];
$bs = $b['size'];
if($as == $bs) { return 0; }
else return $as < $bs ? 1 : -1;
});
 
foreach($data as $x)
{
echo $x['name'] . " " . $x['size'] . "<br>";
}
 
### Linq: ###
 
echo "<br/><br> Linq: <br /><br>";
 
$linq = Linq::from($files)
->select(function($x) { return array("name" => $x, "size" => filesize($x)); })
->orderByDescending(function($x) { return $x['size']; })
->each(function($x) {
echo $x['name'] . " " . $x['size'] . "<br>";
});
/vendor/fusonic/linq/examples/5-grouping.php
@@ -0,0 +1,54 @@
<?php
 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
 
use Fusonic\Linq\Linq;
$files = glob("/tmp/*");
 
// Group all files by its filesize.
 
### Plain PHP: ###
$data = array();
foreach($files as $file) {
$currentSize = filesize($file);
$data[] = array("name" => $file, "size" => $currentSize);
}
 
uasort($data, function($a, $b) {
$as = $a['size'];
$bs = $b['size'];
if($as == $bs) { return 0; }
else return $as < $bs ? 1 : -1;
});
 
$grouped = array();
foreach($data as $x)
{
if(isset($grouped[$x['size']])) {
$grouped[$x['size']][] = $x;
}
else {
$grouped[$x['size']] = array($x);
}
}
 
foreach($grouped as $key => $value) {
echo $key . " (" . count($value) . ")" . "<br />";
foreach($value as $file) {
echo " -" . $file["name"] . "<br>";
}
};
 
### Linq: ###
 
echo "<br/><br> Linq: <br /><br>";
 
$linq = Linq::from($files)
->select(function($x) { return array("name" => $x, "size" => filesize($x)); })
->orderByDescending(function($x) { return $x['size']; })
->groupBy(function($x) { return $x['size']; })
->orderByDescending(function($x) { return $x->count(); })
->each(function($x) {
echo $x->key() . " (" . $x->count() . ")" . "<br />";
$x->each(function($y) { echo " -" . $y["name"] . "<br>"; });
});
/vendor/fusonic/linq/examples/6-typesafe.php
@@ -0,0 +1,27 @@
<?php
/*
* One important design goal was the principle of the least surprise.
* As PHP is a fully dynamic language with nearly no type-safety, it is common to shoot yourself into the foot because of accidentally mixing up incompatible types.
* We protect you from these programing errors by asserting that every callback functions you supply to the library must return a correctly typed value.
* In addition, every supported aggregate function will throw an exception if you are accidentally mixing up incompatible types.
*
* This means that we made this library totally predictable in what it does, and verified that every function has its defined exceptions
* which are thrown when certain operations fail, or if certain types are not correct.
*
*/
echo "<pre>";
 
require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload
 
use Fusonic\Linq\Linq;
 
/* Throws an UnexpectedValueException if the
provided callback function does not return a boolean */
Linq::from(array("1", "1"))
->where(function($x) { return "NOT A BOOLEAN"; });
 
 
/* Throws an UnexpectedValueException if one of the values
is not convertible to a numeric value:*/
Linq::from(array(1, 2, "Not a numeric value"))
->sum();