Strings\repeat()

transformer returns Closure pure
int → (string → string)
At a glance — Bind a repetition count. Wraps str_repeat().

Allows you to create a function which can be used to repeat a string by a pre defined number. These can either be used as part of a Higher Order Function such as array_map() or as part of a compiled/pipe function.

/**
  * @param int $count Number of times to repeat string.
  * @return Closure(string):string
  */
 Strings\repeat(int $count): Closure

Returned Closure

When Strings\repeat() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param string $string The string to repeat
  * @return string
  * @psalm-pure
  */ 
$function(string $string): string

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

// Create a closure which can be used to repeat a string 2 times.
$repeat = Strings\repeat(2);

// Called as a function.
echo $repeat('Hello World'); // Hello WorldHello World

// Used in a higher order function.
$array = array_map( $hello, ['Once upon time', 'Find the time']);
print_r($array); // [Once upon timeOnce upon time, Find the timeFind the time]

Curried

This can be called inline using currying.

echo Strings\repeat(3)('ABC'); //  ABCABCABC

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

$array = array_map( Strings\repeat(3), ['abcd', 'efgh']);
print_r($array); // [abcdabcdabcd, efghefghefgh]

Details

Strings Functions

Releated String manipulation Functions