There is no license information available for the latest version (dev-master) of this package.

dev-master 2018-10-25 06:00 UTC

This package is auto-updated.

Last update: 2025-06-26 22:14:55 UTC


README

f is a functional lib for php

Note: this lib is not optimized for performance yet

Currying

$threeArgs = function($a, $b, $c) {
    return [$a, $b, $c];$expected = 
};
$treeArgsPartial  = \f\partial($threeArgs);
$firstAs666 = $treeArgsPartial(666);
$firstAs666(777, 888);
//[666,777,888]

Fold

$addOne = function($a, $b) {
    return $a+1;
};
$count = \f\fold($addOne, 0);
$count([3, 4, 5, 7]);
//4

Fold Tree

$tree = [
    '01' => [
        '02',
        '03' => [
            '4',
        ],
    ]
];
\f\foldTree($sum, $sum, 0, $tree)
//10

Map Tree

$tree = [
    '03' => [1,4],
    '01' => [1,'05' => [1,2, 3]]
];
$double = function($a) {
    return $a*2;
};
$result  = \f\mapTree($double, $tree);
//[
//    '06' => [2,8],
//    '02' => [2,'010' => [2,4,6]]
//];

Pattern matching

$sumList = \f\patternMatch([
    '[]' => 0,
    '(x:xs)' => function ($x, $xs) use (&$sumList) {
        return $x + $sumList($xs);
    }
]);

$sumList([1,2,3]);
//6

Lazy loading

\f\takeFrom(\f\infinity(), 5)
//[0,1,2,3,4]

Memoize

$calls = 0;
$factorial = function($x) use (&$factorial, &$calls) {
    $calls++;
    if ($x == 1) {
        return 1;
    }
    return $x * $factorial($x - 1);
};

$memoizedFactorial = \f\memoize($factorial);
$memoizedFactorial(4);
$memoizedFactorial(4);
//$calls = 4