Skip to content

Instantly share code, notes, and snippets.

@mirzalazuardi
Last active January 18, 2019 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirzalazuardi/42679565d615e34e540ae56d3441dc0d to your computer and use it in GitHub Desktop.
Save mirzalazuardi/42679565d615e34e540ae56d3441dc0d to your computer and use it in GitHub Desktop.
PHP #2
<?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