public function testCsvParser() { $file = __DIR__ . "/../examples/testdata.csv"; $conv = new ObjectArrayConverter(); $parser = new CsvParser(); $conv->setIteratorSource($parser->openFile(new File($file))); $records = new ArrayList($conv); //$records->each(function($r) { var_dump($r->toArray()); }); $this->assertSame(7, $records->length()); $this->assertSame(2, $records->where(function ($v) { return $v->Col1 == "Bar"; })->length()); $this->assertSame(6, $records->max(function ($v) { return $v->length(); })); unset($records); }
include __DIR__ . "/examples-utils.php"; // Lambda classes use PerrysLambda\ObjectArray as OA; use PerrysLambda\ArrayList as AL; // Stopwatch $total = new Stopwatch(); $watch = new Stopwatch(); $total->start(); // Memory usage on start echo "\n"; L::line("Begin."); // Basic example $watch->start(); $test = new AL(array(1, 2, 3, 4, 5, 6, 7, 8, 9)); L::vdl($test->where(function ($v) { return $v > 5; })->toArray()); L::line("Select values greater than X from simple array", $watch->stop()->result()); echo "\n"; // Parse JSON (testdata) L::line("Filesize of JSON is", number_format(filesize(__DIR__ . "/testdata.json") / 1024, 2), "KB"); $watch->start(); $data = json_decode(file_get_contents(__DIR__ . "/testdata.json"), true); L::line("Loaded data from JSON file.", $watch->stop()->result(), ",", count($data), "records"); // Load JSON data into lambda $watch->start(); $collection = AL::asObjectArray($data); // ArrayList<ObjectArray> L::line("Data imported into Lambda.", $watch->stop()->result(), ",", $collection->length(), "records"); // Unset JSON Data unset($data);
L::line("Data imported:", $watch->stop()->result()); // Stats echo "\n"; L::line($list->length(), "records"); L::line("First record:", $list->first()->timestamp->format('Y-m-d H:i:s')); L::line("Last record:", $list->last()->timestamp->format('Y-m-d H:i:s')); echo "\n"; // Only the newest day, group by hour $watch->start(); $dates = $list->distinct(function ($v) { return $v->timestamp->format('Y-m-d'); })->take(-2)->select(function ($v) { return $v->timestamp->format('Y-m-d'); }); $hours = $list->where(function (ObjectArray $r) use($list, $dates) { return in_array($r->timestamp->format('Y-m-d'), $dates); })->groupBy(function (ObjectArray $r) { return $r->timestamp->format('Y-m-d H:00'); }); L::line("Filter for last day:", $watch->stop()->result()); /** * Print results */ // Table header echo "\n"; echo columnline(array(array(19, "Time"), array(8, "Records"), array(10, "OMin °C"), array(10, "OAvg °C"), array(10, "OMax °C"), array(10, "IMin °C"), array(10, "IAvg °C"), array(10, "IMax °C"), array(8, "Min H%"), array(8, "Avg H%"), array(6, "Max H%"))); // Print hourly records foreach ($hours as $hour => $records) { $columns = array(); $columns[] = array(19, $hour); $columns[] = array(8, $records->length());