Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Core functions, IHash, and Hash
- Loading branch information
1 parent
aec4846
commit 3028c2d
Showing
7 changed files
with
252 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
<?php | ||
require_once '../recess.php'; | ||
require_once '../recess.php'; |