PHP code example of dirtsimple / fun-factory

1. Go to this page and download the library: Download dirtsimple/fun-factory 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/ */

    

dirtsimple / fun-factory example snippets


$f = fun();

$f = function ($arg=null) { return $arg; };

$f = fun('$_ * 2');

$f = function ($arg=null) { return $arg * 2; };

$f1 = fun()->foo($bar)->baz();



$f2 = fun()->aProp[$key];

​

$f1 = function ($arg=null) use ($bar) {
    return $arg->foo($bar)->baz();
};

$f2 = function ($arg=null) use ($key) {
    return $arg->aProp[$key];
};

$f1 = fun('array_flip', 'array_reverse');



$f2 = fun('func', [$ob, 'meth'], '$_*2');

​

$f1 = function ($arg=null) {
    return array_flip(array_reverse($arg));
};

$f2 = function ($arg=null) use ($ob) {
    return func($ob->meth($arg * 2));
};

$set = fun()->offsetSet($foo, $bar);




$unset = fun()->offsetUnset($foo);




$exists = fun()->offsetExists($foo);



​

$set = function ($arg=null) use ($foo, $bar) {
    $arg[$foo] = $bar;
    return $arg;
};

$unset = function ($arg=null) use ($foo) {
    unset($arg[$foo]);
    return $arg;
};

$exists = function ($arg=null) use ($foo) {
    return is_array($arg)
        ? array_key_exists($foo, $arg)
        : $arg->offsetExists($foo);
};

$f = fun(...$callables);
function ($_) use ($f) { $f($_); return $_; }

function ($out, $key, $fn, $input) {
    $out[$key] = $fun($input);
    return $out;
}