Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added materialize, compact and replace functions. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
CHANGELOG
=========

- Added `materialize`, `replace`, and `compact` functions to `Collection`.

0.3
---

Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ Current functions:
- `median`: Return the median value
- `deviation`: Return the deviation as a percentage

Partitioning and Forking
------------------------
Partitioning, Aggregation, and Materialization
----------------------------------------------

`Table` and `Row` instances provide the following methods:

- `partition`: Internally divide the collection of elements according to a
given callback.
- `aggregate`: Aggregate the partitions of a table back to a single partition.
- `materialize`: Materialize partitions of constituent Cellular instances into
Cellular instances.

This is useful for creating summaries from multiple tables or rows:

Expand All @@ -143,6 +145,11 @@ partition for the collection upon which the operation is performed.

The result will be anagous to the following SQL: `SELECT SUM(number) FROM table GROUP BY class`.

Compacting
----------

TODO

Sorting
-------

Expand Down
50 changes: 50 additions & 0 deletions lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,56 @@ public function partition(\Closure $closure)
return $this;
}

/**
* Materialize each partition of the child cellular instances
* into a concrete instance.
*
* @return Collection
*/
public function materialize()
{
$cells = array();
foreach ($this as $cell) {
foreach ($cell->getPartitions() as $partition) {
$cells[] = $cell->create($partition->getElements());
}
}
$this->replace($cells);
return $this;
}

/**
* Aggregate all cells into a single cell.
*
* @return Collection
*/
public function compact()
{
$newCell = null;
foreach ($this as $cell) {
if (null === $newCell) {
$newCell = $cell->create();
}

foreach ($cell as $element) {
$newCell[] = $element;
}
}

$this->replace(array($newCell));
}

/**
* Replace the elements in the collection
*
* @param array $elements
* @return Collection
*/
public function replace($elements)
{
$this->partitions = array(new Partition($elements));
}

/**
* Aggregate the partions back to a single partition using
* the given closure.
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,36 @@ public function testPartition()
),
array_values($partitions[1]->getElements())
);

return $this->collection;
}

/**
* It should marerialize partitions of its consituent cellulars
*
* @depends testPartition
*/
public function testMaterialize($collection)
{
$collection = new Collection(array($collection));
$this->assertCount(1, $collection);
$collection->materialize();
$this->assertCount(2, $collection);

$this->assertEquals(
array(
array('a', 'b'),
array('a', 'a'),
),
$collection[0]->getElements()
);
$this->assertEquals(
array(
array('b', 'a'),
array('b', 'a'),
),
array_values($collection[1]->getElements())
);
}

/**
Expand Down Expand Up @@ -150,6 +180,20 @@ public function testApply()
);
}

/**
* It should replace the elements in the collection
*/
public function testReplace()
{
$collection = new Collection(array(1, 2), array(3, 4));
$collection->replace(array(100, 200, 300));

$this->assertEquals(
array(100, 200, 300),
$collection->getElements()
);
}

/**
* It should map a closure to each element.
*/
Expand Down Expand Up @@ -317,4 +361,18 @@ public function testCopyWithObject()
$this->assertCount(2, $elements);
$this->assertNotSame($elements[0], $object1);
}

/**
* It should compact its constituent elements into a single element
*/
public function testCompact()
{
$this->collection[] = new Collection(array('a', 'b'));
$this->collection[] = new Collection(array('c', 'd'));
$this->collection->compact();
$this->assertCount(1, $this->collection);
$this->assertEquals(array(
'a', 'b', 'c', 'd',
), $this->collection->first()->getElements());
}
}