PHP code example of hopr / pipeable

1. Go to this page and download the library: Download hopr/pipeable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hopr / pipeable example snippets


$arr = [1, 2];
// Assume $filter_function and $reduce_function exist
$filtered = $arr
    |> fn(array $data) => array_filter($data, $filter_function)
    |> fn(array $data) => array_reduce($data, $reduce_function);

$filtered = $arr
    |> filter($filter_function) 
    |> reduce($reduce_function);

function filter(callable $callback): callable
{
    return fn(array $array) => array_filter($array, $callback);
}

// Won't be executed until called later !
$extract_user_email = map(fn(User $user) => $user->email);
// Some logic ...
// Executed here
$email = $extract_user_email($current_user);

use function Hopr\Pipeable\Array\map;
use function Hopr\Pipeable\Array\filter;
use function Hopr\Pipeable\Array\reduce;

$data = [1, 2, 3, 4, 5];

$result = $data
    |> map(fn($x) => $x * 2)      // [2, 4, 6, 8, 10]
    |> filter(fn($x) => $x > 5)   // [6, 8, 10]
    |> reduce(fn($carry, $item) => $item + $carry, 0); // 24

echo $result; // Output: 24

use function Hopr\Pipeable\String\chunk;
use function Hopr\Pipeable\String\map;
use function Hopr\Pipeable\String\join;
use function Hopr\Pipeable\String\toUpper;

$string = "hello world";

$result = $string
    |> chunk(5)               // ["hello", " worl", "d"]
    |> map(fn($x) => $x |> toUpper(...)) // ["HELLO", " WORL", "D"]
    |> join("-");              // "HELLO- WORL-D"

echo $result; // Output: "HELLO- WORL-D"