Skip to content

Instantly share code, notes, and snippets.

@andreaschrist27
Created January 18, 2019 08:16
Show Gist options
  • Save andreaschrist27/2d4aab75639c89ed689b0d1a68351bc6 to your computer and use it in GitHub Desktop.
Save andreaschrist27/2d4aab75639c89ed689b0d1a68351bc6 to your computer and use it in GitHub Desktop.
Pipeline PHP#2
<?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