Skip to content

Instantly share code, notes, and snippets.

@heavenshell
Created September 5, 2010 06:21
Show Gist options
  • Save heavenshell/565801 to your computer and use it in GitHub Desktop.
Save heavenshell/565801 to your computer and use it in GitHub Desktop.
<?php
require_once 'PHP/ListRubyLike.php';
use PHP\ListRubyLike;
echo PHP\LR(array('foo', 'bar'))
->push('baz', 'piyo')
->map(function ($v) { return strtoupper($v); })
->join(', ') . PHP_EOL;
echo PHP\LR(range(1, 5))
->grep(function ($n) { return $n % 2 == 0; })
->map(function ($n) { return $n * $n; })
->sum() . PHP_EOL;
<?php
namespace PHP;
/**
* Array iterator like Ruby.
*
* @author Naoya Ito <i.naoya@gmail.com>
* licence: TBD
*/
class ListRubyLike
{
private $_list = array();
public function __construct(array $array)
{
$this->_list = $array;
}
private function _new(array $array)
{
$class = __CLASS__;
return new $class($array);
}
public function push()
{
$args = func_get_args();
foreach ($args as $v) {
array_push($this->_list, $v);
}
return $this;
}
public function pop()
{
return array_pop($this->_list);
}
public function unshift()
{
$args = func_get_args();
foreach ($args as $v) {
array_unshift($this->_list, $v);
}
return $this;
}
public function shift()
{
return array_shift($this->_list);
}
public function join($glue)
{
return implode($glue, $this->_list);
}
public function first()
{
return $this->_list[0];
}
public function last()
{
return end($this->_list);
}
public function slice($start, $end)
{
return $this->_new(array_slice($this->_list, $start, $end));
}
public function each($cb)
{
if (!is_callable($cb)) {
throw new LogicException('assert');
}
foreach ($this->_list as $v) {
$cb($v);
}
return $this;
}
public function length()
{
return count($this->_list);
}
public function map($cb)
{
if (!is_callable($cb)) {
throw new LogicException('assert');
}
return $this->_new(array_map($cb, $this->_list));
}
public function grep($cb)
{
$grepped = $this->_new(array());
foreach ($this->_list as $v) {
if ($cb($v) === true) {
$grepped->push($v);
}
}
return $grepped;
}
public function find($cb)
{
foreach ($this->_list as $v) {
if ($cb($v) === true) {
return $v;
}
}
return false;
}
public function eachIndex($cb)
{
if (!is_callable($cb)) {
throw new LogicException('assert');
}
foreach($this->_list as $i => $v) {
$cb($i, $v);
}
return $this;
}
public function reduce($cb)
{
return array_reduce($this->_list, $cb);
}
public function sum()
{
return $this->reduce(function ($a, $b) { return $a + $b; });
}
public function dump()
{
var_dump($this);
}
public function to_a()
{
return $this->_list;
}
}
function LR(array $array)
{
return new ListRubyLike($array);
}
<?php
require_once 'lib/lime.php';
require_once 'PHP/ListRubyLike.php';
use PHP\ListRubyLike;
$t = new lime_test();
$list = PHP\LR(array(2, 4, 6, 8, 10));
$t->ok($list instanceof PHP\ListRubyLike);
$t->ok($list->length() === 5);
$t->ok($list->first() === 2);
$t->ok($list->last() === 10);
$t->ok($list->sum() === 30);
$t->ok($list->pop() === 10);
$t->is_deeply($list->to_a(), array(2, 4, 6, 8));
$t->ok($list->shift() === 2);
$t->is_deeply($list->to_a(), array(4, 6, 8));
$t->ok(get_class($list->push(5)) === 'PHP\\ListRubyLike');
$t->ok($list->last() === 5);
$t->is_deeply( $list->to_a(), array(4, 6, 8, 5) );
$t->ok(get_class($list->unshift(10)) === 'PHP\\ListRubyLike');
$t->ok($list->first() === 10);
$t->is_deeply( $list->to_a(), array(10, 4, 6, 8, 5) );
$t->ok($list->join(',') === '10,4,6,8,5');
$t->is_deeply( $list->slice(2, 2)->to_a(), array(6,8));
/* TODO
- each()
- each_index()
- map()
- grep()
- find()
- reduce()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment