scratch – Blame information for rev 87
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
87 | office | 1 | <?php |
2 | |||
3 | require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload |
||
4 | |||
5 | use Fusonic\Linq\Linq; |
||
6 | $files = glob("/tmp/*"); |
||
7 | |||
8 | // Calculate the average filesize of all files greater than 1024 bytes in a directory |
||
9 | // but skip the very first 2 files and then take only 4 files. |
||
10 | |||
11 | ### Plain PHP: ### |
||
12 | |||
13 | $sum = 0; |
||
14 | $i = 0; |
||
15 | $y = 0; |
||
16 | foreach($files as $file) { |
||
17 | $currentSize = filesize($file); |
||
18 | if($currentSize > 1024) { |
||
19 | if($y < 2) { |
||
20 | $y++; |
||
21 | continue; |
||
22 | } |
||
23 | else if ($y > 5) { |
||
24 | break; |
||
25 | } |
||
26 | $y++; |
||
27 | $sum += $currentSize; |
||
28 | $i++; |
||
29 | } |
||
30 | } |
||
31 | $avg = $sum / $i; |
||
32 | |||
33 | echo "Average: " . $avg; |
||
34 | |||
35 | ### Linq: ### |
||
36 | |||
37 | $avgL = Linq::from($files) |
||
38 | ->select(function($x) { return filesize($x); }) |
||
39 | ->where(function($x) { return $x > 1024; }) |
||
40 | ->skip(2) |
||
41 | ->take(4) |
||
42 | ->average(); |
||
43 | |||
44 | echo "<br/><br>Average Linq: " . $avgL; |