Skip to content

Commit

Permalink
Sum, average, min, max and toString added
Browse files Browse the repository at this point in the history
  • Loading branch information
DusanKasan committed Jun 2, 2016
1 parent d4aabe9 commit dd418ed
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 130 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
83 changes: 50 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
```
50 changes: 50 additions & 0 deletions src/CollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
93 changes: 93 additions & 0 deletions src/collection_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
55 changes: 0 additions & 55 deletions src/utility_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Loading

0 comments on commit dd418ed

Please sign in to comment.