Guest User

Untitled

a guest
Jan 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. <?php
  2. class Pipeline {
  3. public static function make_pipeline(...$funcs) {
  4. return function($arg) use ($funcs) {
  5. $output = NULL;
  6.  
  7. if(count($funcs) > 0) {
  8. foreach($funcs as $key => $calculate) {
  9. if(is_callable($calculate)) {
  10. if(is_null($output)) {
  11. $output = $calculate($arg);
  12. } else {
  13. $output = $calculate($output);
  14. }
  15. }
  16. }
  17. }
  18.  
  19. return $output;
  20. };
  21. }
  22. }
  23.  
  24. $fun = Pipeline::make_pipeline(
  25. function($x) { return $x * 3; },
  26. function($x) { return $x + 1; },
  27. function($x) { return $x / 2; }
  28. );
  29.  
  30. echo $fun(3); # should print 5
Add Comment
Please, Sign In to add comment