diff --git a/CHANGELOG.md b/CHANGELOG.md index 0476e56..699dab9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,3 +78,16 @@ - sizeIsGreaterThan function added - sizeIsLessThan function added - sizeIsBetween function added + +##7.0.0 +- The functionality of sum, average, min, max and concatenate moved into collection. +- Sum collection function added +- Average collection function added +- Min collection function added +- Max collection function added +- ToArrray collection function added +- **Breaking change: sum utility function removed** +- **Breaking change: average utility function removed** +- **Breaking change: min utility function removed** +- **Breaking change: max utility function removed** +- **Breaking change: concatenate utility function removed** diff --git a/README.md b/README.md index 1e45014..bc9e0f9 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,16 @@ Collection::from([1, 3, 3, 2]) toArray(append([1, 3, 3, 2], 1, 'key')); //[1, 3, 3, 2, 'key' => 1] ``` +#### average() : double +Returns average of values in this collection. +```php +Collection::from([1, 2, 3, 2])->average(); //2.0 +Collection::from([])->average(); //0.0 +``` +```php +average([1, 2, 3]); //2.0 +``` + #### combine(array|Traversable $collection, bool $strict = false) : Collection Combines the values of this collection as keys, with values of $collection as values. The resulting collection has length equal to the size of smaller collection. If $strict is true, the size of both collections must be equal, otherwise ItemNotFound is thrown. When strict, the collection is realized immediately. ```php @@ -802,6 +812,26 @@ Collection::from([1, 3, 3, 2]) toArray(values(mapcat([1, 3, 3, 2], function ($value) {return [[$value]];}))); //[[1], [3], [3], [2]] ``` +#### max() : mixed +Returns mthe maximal value in this collection. +```php +Collection::from([1, 2, 3, 2])->max(); //3 +Collection::from([])->max(); //null +``` +```php +max([1, 2, 3, 2]); //3 +``` + +#### min() : mixed +Returns mthe minimal value in this collection. +```php +Collection::from([1, 2, 3, 2])->min(); //1 +Collection::from([])->min(); //null +``` +```php +min([1, 2, 3, 2]); //1 +``` + #### only(array|Traversable $keys) : Collection Returns a lazy collection of items associated to any of the keys from $keys. ```php @@ -1136,6 +1166,16 @@ Collection::from([1, 3, 3, 2]) toArray(splitWith([1, 3, 3, 2], function ($value) {return $value < 3;})); //[[1], [1 => 3, 2 => 3, 3 => 2]] ``` +#### sum() : double +Returns a sum of all values in this collection. +```php +Collection::from([1, 2, 3])->sum(); //6 +Collection::from([])->sum(); //0 +``` +```php +sum([1, 2, 3]); //6 +``` + #### take(int $numberOfItems) : Collection A form of slice that returns first $numberOfItems items. ```php @@ -1204,6 +1244,16 @@ Collection::from([1, 3, 3, 2])->toArray(); //[1, 3, 3, 2] toArray([1, 3, 3, 2]); //[1, 3, 3, 2] ``` +#### toString() : string +Returns a string by concatenating this collection's values into a string. +```php +Collection::from([1, 'a', 3, null])->toString(); //'1a3' +Collection::from([])->toString(); //'' +``` +```php +toString([1, 'a', 3, null]); //'1a3' +``` + #### zip(array|Traversable[] ...$collections) : Collection Returns a lazy collection of non-lazy collections of items from nth position from this collection and each passed collection. Stops when any of the collections don't have an item at the nth position. ```php @@ -1251,36 +1301,3 @@ Returns value of $value decremented by one. ```php decrement(2) === 1; //true ``` - -#### sum(... int $values) -Returns the sum of all arguments. - -```php -sum(1, 2, 3) === 6; //true -``` - -#### max(... int $values) -Returns the maximal value of all arguments. - -```php -max(1, 5, 3) === 5; //true -``` - -#### min(... int $values) -Returns the minimal value of all arguments. - -```php -min(1, 5, 3) === 1; //true -``` -#### average(... int $values) -Returns the average value of all arguments. - -```php -average(1, 2, 3) === 2.0; //true -``` - -#### concatenate(... string $values) -Returns a string of all the arguments concatenated together. -```php -concatenate('a', 'b', 'c') == 'abc'; //true -``` diff --git a/src/CollectionTrait.php b/src/CollectionTrait.php index af13619..c5f247b 100644 --- a/src/CollectionTrait.php +++ b/src/CollectionTrait.php @@ -806,6 +806,56 @@ public function sizeIsBetween($fromSize, $toSize) return sizeIsBetween($this->getItems(), $fromSize, $toSize); } + /** + * Returns a sum of all values in this collection. + * + * @return double + */ + public function sum() + { + return sum($this->getItems()); + } + + /** + * Returns average of values from this collection. + * + * @return double + */ + public function average() + { + return average($this->getItems()); + } + + /** + * Returns maximal value from this collection. + * + * @return mixed + */ + public function max() + { + return \DusanKasan\Knapsack\max($this->getItems()); + } + + /** + * Returns minimal value from this collection. + * + * @return mixed + */ + public function min() + { + return \DusanKasan\Knapsack\min($this->getItems()); + } + + /** + * Returns a string by concatenating the collection values into a string. + * + * @return string + */ + public function toString() + { + return toString($this->getItems()); + } + /** * @return array|\Traversable */ diff --git a/src/collection_functions.php b/src/collection_functions.php index b1796a2..33677de 100644 --- a/src/collection_functions.php +++ b/src/collection_functions.php @@ -1516,3 +1516,96 @@ function sizeIsBetween($collection, $fromSize, $toSize) return $fromSize < $itemsTempCount && $itemsTempCount < $toSize; } + +/** + * Returns a sum of all values in the $collection. + * + * @param array|Traversable $collection + * @return float + */ +function sum($collection) +{ + $result = 0.0; + + foreach ($collection as $value) { + $result += $value; + } + + return $result; +} + +/** + * Returns average of values from $collection. + * + * @param array|Traversable $collection + * @return float + */ +function average($collection) +{ + $sum = 0.0; + $count = 0; + + foreach ($collection as $value) { + $sum += $value; + $count++; + } + + return $count ? (float) $sum/$count : 0.0; +} + +/** + * Returns maximal value from $collection. + * + * @param array|Traversable $collection + * @return mixed + */ +function max($collection) +{ + $result = null; + + foreach ($collection as $value) { + $result = $value > $result ? $value : $result; + } + + return $result; +} + +/** + * Returns minimal value from $collection. + * + * @param array|Traversable $collection + * @return mixed + */ +function min($collection) +{ + $result = null; + $hasItem = false; + + foreach ($collection as $value) { + if (!$hasItem) { + $hasItem = true; + $result = $value; + } + + $result = $value < $result ? $value : $result; + } + + return $result; +} + +/** + * Returns a string by concatenating the $collection values into a string. + * + * @param array|Traversable $collection + * @return string + */ +function toString($collection) +{ + $result = ''; + + foreach ($collection as $value) { + $result .= (string) $value; + } + + return $result; +} diff --git a/src/utility_functions.php b/src/utility_functions.php index 1d3ee6f..b71228b 100644 --- a/src/utility_functions.php +++ b/src/utility_functions.php @@ -51,58 +51,3 @@ function decrement($value) { return $value - 1; } - -/** - * Returns a sum of all arguments. - * - * @param int|float|double[] ...$values - * @return number - */ -function sum(...$values) -{ - return array_sum($values); -} - -/** - * Returns the highest value from all arguments. - * - * @param int|float|double[] ...$values - * @return mixed - */ -function max(...$values) -{ - return \max($values); -} - -/** - * Returns the lowest value from all arguments. - * - * @param int|float|double[] ...$values - * @return mixed - */ -function min(...$values) -{ - return \min($values); -} - -/** - * Returns the average of all arguments. - * - * @param int|float|double[] ...$values - * @return float - */ -function average(...$values) -{ - return sum(...$values) / count($values); -} - -/** - * Concatenates all arguments. - * - * @param string[] ...$values - * @return mixed - */ -function concatenate(...$values) -{ - return implode('', $values); -} diff --git a/tests/spec/CollectionSpec.php b/tests/spec/CollectionSpec.php index a563d29..d138517 100644 --- a/tests/spec/CollectionSpec.php +++ b/tests/spec/CollectionSpec.php @@ -1098,48 +1098,6 @@ function it_can_get_the_intersect_of_collections() $this->intersect([1], [3])->values()->toArray()->shouldReturn([1, 3]); } - function it_can_use_the_utility_methods() - { - $this->beConstructedWith([1, 3, 2]); - - $this - ->sort('\DusanKasan\Knapsack\compare') - ->values() - ->toArray() - ->shouldReturn([1, 2, 3]); - - $this - ->map('\DusanKasan\Knapsack\compare') - ->toArray() - ->shouldReturn([1, 1, 0]); - - $this - ->map('\DusanKasan\Knapsack\decrement') - ->toArray() - ->shouldReturn([0, 2, 1]); - - $this - ->reduce('\DusanKasan\Knapsack\sum', 0) - ->shouldReturn(9); - - $this - ->reduce('\DusanKasan\Knapsack\max', 0) - ->shouldReturn(3); - - $this - ->reduce('\DusanKasan\Knapsack\min', 1) - ->shouldReturn(0); - - $this - ->map('\DusanKasan\Knapsack\average') - ->toArray() - ->shouldReturn([0.5, 2, 2]); - - $this - ->reduce('\DusanKasan\Knapsack\concatenate', '') - ->shouldReturn('103122'); - } - function it_can_check_if_size_is_exactly_n() { $this->beConstructedWith([1, 2]); @@ -1172,5 +1130,75 @@ function it_can_check_if_size_is_between_n_and_m() $this->sizeIsBetween(0, 0)->shouldReturn(false); $this->sizeIsBetween(3, 1)->shouldReturn(true); } + + function it_can_sum_the_collection() + { + $this->beConstructedWith([1, 2, 3, 4]); + $this->sum()->shouldReturn(10.0); + } + + function it_can_get_average_of_the_collection() + { + $this->beConstructedWith([1, 2, 2, 3]); + $this->average()->shouldReturn(2.0); + } + + function it_will_return_zero_when_average_is_called_on_empty_collection() + { + $this->beConstructedWith([]); + $this->average()->shouldReturn(0.0); + } + + function it_can_get_maximal_value_in_the_colleciton() + { + $this->beConstructedWith([1, 2, 3, 2]); + $this->max()->shouldReturn(3); + } + + function it_will_return_null_when_max_is_called_on_empty_collection() + { + $this->beConstructedWith([]); + $this->max()->shouldReturn(null); + } + + function it_can_get_min_value_in_the_colleciton() + { + $this->beConstructedWith([2, 1, 3, 2]); + $this->min()->shouldReturn(1); + } + + function it_will_return_null_when_min_is_called_on_empty_collection() + { + $this->beConstructedWith([]); + $this->min()->shouldReturn(null); + } + + function it_can_convert_the_collection_to_string() + { + $this->beConstructedWith([2, 'a', 3, null]); + $this->toString()->shouldReturn('2a3'); + } + + function it_can_use_the_utility_methods() + { + $this->beConstructedWith([1, 3, 2]); + + $this + ->sort('\DusanKasan\Knapsack\compare') + ->values() + ->toArray() + ->shouldReturn([1, 2, 3]); + + $this + ->map('\DusanKasan\Knapsack\compare') + ->toArray() + ->shouldReturn([1, 1, 0]); + + $this + ->map('\DusanKasan\Knapsack\decrement') + ->toArray() + ->shouldReturn([0, 2, 1]); + } + }