Strings\prepend()

transformer returns Closure pure
string → (string → string)
At a glance — Bind a prefix; the returned Closure prepends it to any string.

Allows you to create a function which can be used to prepend a sub string to a passed string. This can either be used as part of a Higher Order Function such as array_map() or as part of a compiled/pipe function.

/**
  * @param string $prepend
  * @return Closure(string):string
  */
 Strings\prepend(string $prepend): Closure

Returned Closure

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

/**
  * @param string $toPrependOnto  The string to have the sub string added to
  * @return string                The sliced string
  * @psalm-pure
  */ 
$function(string $toPrependOnto): string

Examples

Partial Application

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

// Creates the Closure.
$prependFoo = Strings\prepend('foo');  

// Called as a function.  
echo $prependFoo('Hello'); // fooHello  

// Used in a higher order function.  
$array = array_map( $prependFoo, ['Hello', 'World']);  
print_r($array); // [fooHello, fooWorld]  

Curried

This can be called inline using currying.

echo Strings\prepend('foo')('Bar'); // fooBar

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\prepend('--'), ['foo', 'bar'] );
print_r($array); // ['--foo', '--bar']  

Details

Strings Functions

Releated String manipulation Functions