PHP code example of wojciech.nawalaniec / phlambda

1. Go to this page and download the library: Download wojciech.nawalaniec/phlambda 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/ */

    

wojciech.nawalaniec / phlambda example snippets


use Wojciech\Phlambda as f;

f\all(f\above(2), [1, 2, 3, 4]);

use function Wojciech\Phlambda\{all, above};

all(above(2), [1, 2, 3, 4]);

use function Wojciech\Phlambda\map;
use const Wojciech\Phlambda\toString;

map(toString, [1, 2, 3]);

use function Wojciech\Phlambda\{_, below, concat};
use const Wojciech\Phlambda\toString;

_($someArray)
    ->filter(below(30))
    ->map(toString) // you can use constant which will be resolved as callable by PHP
    ->reduce(concat(), ''); // or you can call function without params because all functions all curried

// we can use reduce function normally like this:
$array = ['a', 'b', 'c'];
$result = reduce(\Wojciech\Phlambda\concat, '', $array); // $result = 'abc'

// and we can use it like currying function:
$concat = reduce(\Wojciech\Phlambda\concat, ''); // now it will return callback accepting last param from reduce - an array
$result = $concat($array); // $result = 'abc'

$array = [1, 2, 3];
$reducer = reduce(__, __, $array);

$sum = $reducer(sum, 0);
$multiplication = $reducer(multiply, 0);

function below(int $val): callable
{
    return fn(int $v) => $v < $val;
}

function every(array $a, callable $fn): array
{
    return array_filter($a, $fn);
}

$arr = [1, 2, 3, 4];
every($arr, below(3));

$arrayObject
    ->filter(below(30))
    ->sum();