Created
January 18, 2019 08:16
-
-
Save andreaschrist27/2d4aab75639c89ed689b0d1a68351bc6 to your computer and use it in GitHub Desktop.
Pipeline PHP#2
This file contains hidden or 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) | |
{ | |
if(!isset($output)) | |
{ | |
$output = $func($arg); | |
}else{ | |
$output = $func($output); | |
} | |
} | |
return $output; | |
}; | |
} | |
} | |
$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