Last active
July 29, 2020 13:22
-
-
Save omitobi/24b43f6eb275368f9cbbac48fd159fb5 to your computer and use it in GitHub Desktop.
Replicate a value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Replicate a value | |
* | |
* @param $value | |
* @param int $times - ps: modify it to handle values less than 2 | |
* @param bool $handleClosure | |
* @return array | |
*/ | |
function replicate($value, int $times = 2, bool $handleClosure = true): array | |
{ | |
$isClosure = $value instanceof \Closure; | |
$mapClosure = fn() => $isClosure && $handleClosure ? $value() : $value; | |
return array_map($mapClosure, range(1, $times)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment