Skip to content

Instantly share code, notes, and snippets.

@taiki45
Created October 10, 2012 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taiki45/3867655 to your computer and use it in GitHub Desktop.
Save taiki45/3867655 to your computer and use it in GitHub Desktop.
practice for php.
<?php
class RbArray
{
function __construct(){
$this->array = $this->get_val(func_get_args());
}
private function get_val($val){
if(is_array($val) && is_array($val[0])){
return $this->get_val($val[0]);
} else {
return $val;
}
}
public function each($func){
foreach($this->array as $item){
$func($item);
}
return $this;
}
public function map($func){
return new $this(array_map($func, $this->array));
}
public function reduce($init, $func){
return array_reduce($this->array, $func, $init);
}
public function reverse(){
return new $this(array_reverse($this->array));
}
}
$a = new RbArray(1, 2, 3, 4, 5);
$a->reverse()
->map(function($x){return $x * $x;})
->each(function($i){print($i."¥n");});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment