Skip to content

Commit

Permalink
Adding Core functions, IHash, and Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisJordan committed Feb 1, 2010
1 parent aec4846 commit 3028c2d
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 12 deletions.
26 changes: 16 additions & 10 deletions lib/Recess/Core/ClassLoader.php
Expand Up @@ -117,7 +117,7 @@ static private function loader() {
$extension = self::$extension;
self::$loader = function($fullyQualifiedClass) use (&$onLoad, &$extension) {
if(class_exists($fullyQualifiedClass, false)) { return true; }

$class = $fullyQualifiedClass;
$namespace = '';

Expand All @@ -131,17 +131,23 @@ static private function loader() {
str_replace('_', DIRECTORY_SEPARATOR, $class) .
$extension;

if(file_exists($classFile) && is_readable($classFile)) {
require $classFile;
} else {
return false;
$paths = explode(PATH_SEPARATOR, get_include_path());
$found = false;
foreach($paths as $path) {
$classFilePath = $path.DIRECTORY_SEPARATOR.$classFile;
if(file_exists($classFilePath) && is_readable($classFilePath)) {
$found = true;
require $classFilePath;
}
}

if(class_exists($fullyQualifiedClass, false)) {
$onLoad($fullyQualifiedClass);
return true;
if($found === false) return false;

$onLoad($fullyQualifiedClass);
return true;
if(class_exists($fullyQualifiedClass, false)
|| interface_exists($fullyQualifiedClass, false)) {
} else {
throw new \Exception("'$classFile' does not contain definition for $class.");
throw new \Exception("'$classFile' does not contain a definition of $class.");
}
};
}
Expand Down
85 changes: 85 additions & 0 deletions lib/Recess/Core/Hash.php
@@ -0,0 +1,85 @@
<?php
namespace Recess\Core;

class Hash implements IHash {

protected $elements;

function __construct($elements = array()) {
$arguments = func_get_args();
if(count($arguments) > 1 || !is_array($elements)) {
$this->elements = $arguments;
} else {
$this->elements = $elements;
}
}

function toArray() {
return $this->elements;
}

function map($callable) {
return new Hash(map($this->elements,$callable));
}

function reduce($callable, $identity) {
return reduce($this->elements, $callable, $identity);
}

function each($callable) {
each($this->elements, $callable);
return $this;
}

function filter($callable) {
return new Hash(filter($this->elements, $callable));
}

function offsetExists($offset) {
return isset($this->elements[$offset]);
}

function offsetGet($offset) {
return $this->elements[$offset];
}

function offsetSet($offset, $value) {
$this->elements[$offset] = $value;
}

function offsetUnset($offset) {
unset($this->elements[$offset]);
}

/* From \Countable */
function count() {
return count($this->elements);
}

/* From \Iterator */
function current() {
return current($this->elements);
}

function key() {
return key($this->elements);
}

function next() {
return next($this->elements);
}

function rewind() {
return reset($this->elements);
}

function valid() {
return key($this->elements) !== NULL;
}

/* From \IteratorAggregate */
function getIterator() {
return new \ArrayIterator($this->elements);
}

}
34 changes: 34 additions & 0 deletions lib/Recess/Core/IHash.php
@@ -0,0 +1,34 @@
<?php
namespace Recess\Core;

interface IHash extends \ArrayAccess, \Countable, \Iterator, \IteratorAggregate {

/* Hash Specific Functions */
function toArray();

function map($callable);
function reduce($callable,$identity);
function each($callable);
function filter($callable);

/*
From \ArrayAccess
function offsetExists($offset);
function offsetGet($offset);
function offsetSet($offset, $value);
function offsetUnset($offset);
From \Countable
function count();
From \Iterator
function current();
function key();
function next();
function rewind();
function valid();
From \IteratorAggregate
function getIterator();
*/
}
40 changes: 40 additions & 0 deletions lib/Recess/Core/functions.php
@@ -0,0 +1,40 @@
<?php
namespace Recess\Core;

function map($array, $mapFn) {
$results = array();
foreach($array as $value) {
$results[] = $mapFn($value);
}
return $results;
}

function reduce($array, $combineFn, $identity) {
if(count($array) <= 1) {
$out = $identity;
} else if(count($array) > 1) {
$out = array_shift($array);
do {
$next = array_shift($array);
$out = $combineFn($out, $next);
} while(!empty($array));
}
return $out;
}

function each($array, $fn) {
foreach($array as $value) {
$fn($value);
}
return $array;
}

function filter($array, $filterFn) {
$results = array();
foreach($array as $key => $value) {
if($filterFn($value) === true) {
$results[$key] = $value;
}
}
return $results;
}
5 changes: 4 additions & 1 deletion lib/Recess/recess.php
@@ -1,5 +1,8 @@
<?php
set_include_path(realpath(__DIR__ . '/../') . PATH_SEPARATOR . get_include_path());
set_include_path(
realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR)
. PATH_SEPARATOR . get_include_path());

require 'Recess/Core/functions.php';
require 'Recess/Core/ClassLoader.php';
spl_autoload_register('Recess\Core\ClassLoader::load');
72 changes: 72 additions & 0 deletions lib/Recess/tests/Recess/Core/HashTest.php
@@ -0,0 +1,72 @@
<?php
require_once 'PHPUnit/Framework.php';

include_once __DIR__ . '/../../../../Recess/Core/Hash.php';
use Recess\Core\Hash;

class HashTest extends PHPUnit_Framework_TestCase {

function testEmptyConstructor() {
$hash = new Hash();
$this->assertEquals(0,count($hash));
}

function testArrayConstructor() {
$hash = new Hash(array(1,2,3,4));
$this->assertEquals(4,count($hash));
}

function testListConstructor() {
$hash = new Hash(1,2,3,4);
$this->assertEquals(4,count($hash));
}

function testSingleArgumentListConstructor() {
$hash = new Hash(1);
$this->assertEquals(1,count($hash));
}

function testMap() {
$hash = new Hash(1,2,3,4);
$mapped = $hash->map(function($value) {
return $value * $value;
});
$this->assertEquals(array(1,4,9,16),$mapped->toArray());
}

function testReduce() {
$hash = new Hash(1,2,3,4);
$sum = $hash->reduce(function($a,$b) { return $a + $b; }, 0);
$this->assertEquals(10,$sum);
}

function testReduceIdentity() {
$hash = new Hash();
$sum = $hash->reduce(function($a,$b){},0);
$this->assertEquals(0,$sum);
}

function testEach() {
$hash = new Hash(1,2,3,4);
$count = 0;
$hash->each(function($item)use(&$count){$count += 1;});
$this->assertEquals(4,$count);
}

function testFilter() {
$hash = new Hash(1,2,3,4);
$even = $hash->filter(function($value) { return $value % 2 === 0; });
$this->assertEquals(array(1=>2,3=>4),$even->toArray());
}

function testForeach() {
$hash = new Hash(1,2,3,4);
$count = 0;
foreach($hash as $key => $value) {
$count += 1;
$this->assertEquals($key + 1, $value);
}
$this->assertEquals(4,$count);
}

}
2 changes: 1 addition & 1 deletion lib/Recess/tests/bootstrap.php
@@ -1,2 +1,2 @@
<?php
require_once '../recess.php';
require_once '../recess.php';

0 comments on commit 3028c2d

Please sign in to comment.