Last active
January 18, 2019 05:01
-
-
Save mirzalazuardi/42679565d615e34e540ae56d3441dc0d to your computer and use it in GitHub Desktop.
PHP #2
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 | |
/**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 function with the result of the first function. | |
- The returned function should continue calling each function in the make_pipeline in order, following the same pattern, and return the value from the last function. | |
For example, Pipeline::make_pipeline(function($x) { return $x * 3; }, function($x) { return $x + 1; }, function($x) { return $x / 2; }) then calling the returned function with 3 should return 5. | |
**/ | |
class Pipeline | |
{ | |
public static function make_pipeline() | |
{ | |
$funcs = func_get_args(); | |
return function($arg) use ($funcs) | |
{ | |
foreach($funcs as $function) { | |
$result = (!isset($result)) ? $function($arg) : $function($result); | |
} | |
return $result; | |
}; | |
} | |
} | |
$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