Skip to content

Instantly share code, notes, and snippets.

@omitobi
Last active July 29, 2020 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omitobi/24b43f6eb275368f9cbbac48fd159fb5 to your computer and use it in GitHub Desktop.
Save omitobi/24b43f6eb275368f9cbbac48fd159fb5 to your computer and use it in GitHub Desktop.
Replicate a value
<?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