Skip to content

FranklyRocks/sharp-pipes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

PHP Pipes

PHP pipes in 156 bytes

Usage

// Equivalent to: function3(function2(function1($v)))

pipe($v, 
    fn($v) => function1($v), 
    fn($v) => function2($v), 
    fn($v) => function3($v),
    ...
); 

Example

function slug(string $s) {
    return pipe($s, 
      fn($s) => strtolower($s), 
      fn($s) => str_replace(" ", "-", $s)
    );
}

slug("10 WAYS to EAT more HEALTHY"); // 10-ways-to-eat-more-healthy

Code explained

class Pipe {
    public static function new(...$args) {
        
        // Retrive first argument as initial value
        $value = array_shift($args);
        
        // Call the functions passing $value and assign its return
        foreach($args as $func) $value = $func($value);
        
        // Return computed value
        return $value;
        
    }
}

// Helper function
function pipe() {
    return Pipe::new(...func_get_args());
}