Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Last active December 16, 2015 08:19
Show Gist options
  • Save tanakahisateru/5404938 to your computer and use it in GitHub Desktop.
Save tanakahisateru/5404938 to your computer and use it in GitHub Desktop.
Ginq https://github.com/akanehara/ginq の何がいいのかを簡単に理解するためのテストプログラム
<?php
require_once dirname(__DIR__) . '/src/Ginq.php';
/**
* Class OreOreGinqTest
*/
class OreOreGinqTest extends PHPUnit_Framework_TestCase {
public function testGinqSugoi()
{
$log = array();
$stream = new OreOreArrayIterator(
range(1, 4),
function ($v) use (&$log) {
$log[] = "fetch $v";
}
);
/* もし PHP5.5 なら...
$streamGenerator = function() use(&log) {
foreach (range(1, 4) as $v) {
$log[] = "fetch $v";
yield $v;
}
};
$stream = $streamGenerator();
*/
$ginq = Ginq::from($stream)
->where(function($v) use (&$log) {
$log[] = "where $v";
return $v < 4;
})
->select(function($v) use (&$log) {
$log[] = "select $v";
return $v * $v;
});
// ここでまだ何も評価されていない
$log[] = 'before reduce';
$result = $ginq->reduce(function($a, $b) use (&$log) {
$log[] = "reduce $a, $b";
return $a + $b;
});
// ちなみにもっと基本的な使い方はこう: foreach ($ginq as $v) { echo $v; }
$log[] = 'after reduce';
// このコール順でバッファなし計算をしているのがわかるかな
$this->assertEquals(array(
'before reduce',
'fetch 1', 'where 1', 'select 1',
'fetch 2', 'where 2', 'select 2', 'reduce 1, 4',
'fetch 3', 'where 3', 'select 3', 'reduce 5, 9',
'fetch 4', 'where 4',
'after reduce',
), $log);
$this->assertEquals(14, $result);
}
}
/**
* Class OreOreArrayIterator
*
* ジェネレーターがないので代わりにイテレーターを実装
*/
class OreOreArrayIterator implements Iterator {
protected $keys;
protected $values;
protected $index;
protected $cached;
protected $curentValue;
protected $logger;
public function __construct(array $src, $logger)
{
$this->keys = array_keys($src);
$this->values = array_values($src);
$this->index = 0;
$this->cached = false;
$this->logger = $logger;
}
public function current()
{
if (!$this->cached) {
// 5.5のyieldにあたる部分、ここをいつ通るかという問題ですよ。
$this->curentValue = $this->values[$this->index];
$this->cached = true;
call_user_func($this->logger, $this->curentValue);
}
return $this->curentValue;
}
public function next()
{
$this->cached = false;
$this->index++;
}
public function key()
{
$this->keys[$this->index];
}
public function valid()
{
return $this->index < count($this->keys);
}
public function rewind()
{
$this->index = 0;
$this->cached = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment