Skip to content

Commit

Permalink
Adds functions to pad and to reverse lists
Browse files Browse the repository at this point in the history
  • Loading branch information
pitchart committed Jul 31, 2018
1 parent 2fabfc7 commit 117a5d9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
const map = '\Pitchart\Phunktional\map';
const reject = '\Pitchart\Phunktional\reject';
const sort = '\Pitchart\Phunktional\sort';
const reverse = '\Pitchart\Phunktional\reverse';
const pad = '\Pitchart\Phunktional\pad';

/**
* Runs a boolean function on each element and only puts those that pass into the output.
Expand Down Expand Up @@ -338,3 +340,34 @@ function partition(int $size)
return \array_chunk($array, $size);
};
}

/**
* Reverses a list
*
* @return \Closure
*
* @see \array_reverse()
*/
function reverse()
{
return function (array $array) {
return \array_reverse($array);
};
}

/**
* Pad array to the specified length with a value
*
* @param int $size
* @param $value
*
* @return \Closure
*
* @see \array_pad()
*/
function pad(int $size, $value)
{
return function (array $array) use ($size, $value) {
return \array_pad($array, $size, $value);
};
}
4 changes: 4 additions & 0 deletions tests/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,9 @@ public function arrayFunctionsBuildersProvider()
yield from ['reject constant' => [(p\filter)($filtering)]];
yield from ['sort' => [p\sort(function (int $a, int $b) { return $a > $b ? 1 : ($b > $a ? -1 : 0); })]];
yield from ['sort constant' => [(p\sort)(function (int $a, int $b) { return $a > $b ? 1 : ($b > $a ? -1 : 0); })]];
yield from ['reverse' => [p\reverse()]];
yield from ['reverse constant' => [(p\reverse)()]];
yield from ['pad' => [p\pad(10, 0)]];
yield from ['pad constant' => [(p\pad)(10, 0)]];
}
}

0 comments on commit 117a5d9

Please sign in to comment.