Skip to content

Commit 117a5d9

Browse files
committed
Adds functions to pad and to reverse lists
1 parent 2fabfc7 commit 117a5d9

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/arrays.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
const map = '\Pitchart\Phunktional\map';
2121
const reject = '\Pitchart\Phunktional\reject';
2222
const sort = '\Pitchart\Phunktional\sort';
23+
const reverse = '\Pitchart\Phunktional\reverse';
24+
const pad = '\Pitchart\Phunktional\pad';
2325

2426
/**
2527
* Runs a boolean function on each element and only puts those that pass into the output.
@@ -338,3 +340,34 @@ function partition(int $size)
338340
return \array_chunk($array, $size);
339341
};
340342
}
343+
344+
/**
345+
* Reverses a list
346+
*
347+
* @return \Closure
348+
*
349+
* @see \array_reverse()
350+
*/
351+
function reverse()
352+
{
353+
return function (array $array) {
354+
return \array_reverse($array);
355+
};
356+
}
357+
358+
/**
359+
* Pad array to the specified length with a value
360+
*
361+
* @param int $size
362+
* @param $value
363+
*
364+
* @return \Closure
365+
*
366+
* @see \array_pad()
367+
*/
368+
function pad(int $size, $value)
369+
{
370+
return function (array $array) use ($size, $value) {
371+
return \array_pad($array, $size, $value);
372+
};
373+
}

tests/ArrayTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,9 @@ public function arrayFunctionsBuildersProvider()
220220
yield from ['reject constant' => [(p\filter)($filtering)]];
221221
yield from ['sort' => [p\sort(function (int $a, int $b) { return $a > $b ? 1 : ($b > $a ? -1 : 0); })]];
222222
yield from ['sort constant' => [(p\sort)(function (int $a, int $b) { return $a > $b ? 1 : ($b > $a ? -1 : 0); })]];
223+
yield from ['reverse' => [p\reverse()]];
224+
yield from ['reverse constant' => [(p\reverse)()]];
225+
yield from ['pad' => [p\pad(10, 0)]];
226+
yield from ['pad constant' => [(p\pad)(10, 0)]];
223227
}
224228
}

0 commit comments

Comments
 (0)