Last active
December 23, 2024 21:23
-
-
Save kenjipm/265194faf8574a5bbb0fad970e926a21 to your computer and use it in GitHub Desktop.
As part of a data processing pipeline, complete the implementation of the make_pipeline method: The method should accept a variable number of functions, and it should return a new function that accepts one parameter $arg. The returned function should call the first function in the make_pipeline with the parameter $arg, and call the second functi…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Pipeline | |
{ | |
public static function make_pipeline(...$funcs) | |
{ | |
return function($arg) use ($funcs) | |
{ | |
foreach ($funcs as $func) | |
{ | |
$arg = $func($arg); | |
} | |
return $arg; | |
}; | |
} | |
} | |
$fun = Pipeline::make_pipeline(function($x) { return $x * 3; }, function($x) { return $x + 1; }, | |
function($x) { return $x / 2; }); | |
echo $fun(3); # should print 5 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment