Skip to content

Instantly share code, notes, and snippets.

@azulkipli
Last active January 4, 2019 16:57
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 azulkipli/285d157be09693b77fcfaaaf80201554 to your computer and use it in GitHub Desktop.
Save azulkipli/285d157be09693b77fcfaaaf80201554 to your computer and use it in GitHub Desktop.
JDS_PHP#2
<?php
class Pipeline
{
public static function make_pipeline(...$funcs)
{
return function($arg) use ($funcs)
{
# calling each function in the make_pipeline in order
foreach($funcs as $function) {
# Store the triggering value in first function of args
if(!isset($value))
$value = $function($arg);
# Subsequent executions will now use the stored value and reset it simultaneously
else
$value = $function($value);
}
# Send back this value
return $value;
};
}
}
$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