Skip to content

Instantly share code, notes, and snippets.

@nezarfadle
Last active May 3, 2024 06:23
Show Gist options
  • Save nezarfadle/3093175ee45da2089d1d86fd3ff7ab70 to your computer and use it in GitHub Desktop.
Save nezarfadle/3093175ee45da2089d1d86fd3ff7ab70 to your computer and use it in GitHub Desktop.
function pipe( ...$transformers )
{
    return function( $value ) use( $transformers )
    {
        return array_reduce( 
            $transformers, 
            fn( $buffer, $transformer ) => $transformer( $buffer ), 
            $value
        );
    };
}

$transform = pipe( 
            fn( $text ) => strrev( $text ) ,
            fn( $text ) => strtoupper( $text ),
        );
       
echo $transform( 'This text will be reversed and capitalize' ), "\r\n";
echo $transform( 'This text will be reversed and capitalize also!!' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment