Write a PHP program to create a new function that composes multiple functions into a single callable
<?php //Licence: https://bit.ly/2CFA5XY function compose(...$functions) { return array_reduce( $functions, function ($carry, $function) { return function ($x) use ($carry, $function) { return $function($carry($x)); }; }, function ($x) { return $x; } ); } $compose = compose( // add 2 function ($x) { return $x + 2; }, // multiply 4 function ($x) { return $x * 4; } ); print_r($compose(2)); echo("\n"); print_r($compose(3)); ?>
Sample Output:
16 20
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Sample Output:
need an explanation for this answer? contact us directly to get an explanation for this answer