Skip to content

Commit

Permalink
Add Call::nTimes() as no-brainer alternative for for
Browse files Browse the repository at this point in the history
  • Loading branch information
paranoiq committed Nov 19, 2020
1 parent b8a77e2 commit e9cbb90
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/common/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Call
/**
* Call function with each given value as param
*
* @param callable $function
* @param callable $function ($value, $key)
* @param iterable|mixed[] $values
*/
public static function with(callable $function, iterable $values): void
Expand All @@ -29,7 +29,7 @@ public static function with(callable $function, iterable $values): void
/**
* Call function with each set of given arguments
*
* @param callable $function
* @param callable $function (...$values, $key)
* @param iterable|mixed[][] $arguments
*/
public static function withArgs(callable $function, iterable $arguments): void
Expand All @@ -40,4 +40,21 @@ public static function withArgs(callable $function, iterable $arguments): void
}
}

/**
* Call given function n times. Syntactic sugar for simple `for (...) {...}`
*
* @param callable $function (int $i)
* @param int $n
* @return mixed[]
*/
public static function nTimes(callable $function, int $n): array
{
$results = [];
for ($i = 0; $i < $n; $i++) {
$results[] = $function($i);
}

return $results;
}

}

0 comments on commit e9cbb90

Please sign in to comment.