PHP code example of zonuexe / functools
1. Go to this page and download the library: Download zonuexe/functools 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/ */
zonuexe / functools example snippets
use Teto\Functools as f;
$comma = f::partial("implode", [", "]);
$comma(range(1, 10));
// "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
$join_10 = f::partial("implode", [1 => range(1, 10)], 0);
$join_10("@");
// "1@2@3@4@5@6@7@8@9@10"
$join_10("\\");
=> "1\\2\\3\\4\\5\\6\\7\\8\\9\\10"
$sleep3 = f::partial("sleep", [3]);
$sleep3();
$sleep3("foo"); // Error!
$sleep3 = f::partial("sleep", [3], -1);
$sleep3("foo"); // OK!
$add = f::op("+");
$add(2, 3); // (2 + 3) === 5
$add_1 = f::op("+", [1]);
$add_1(4); // (1 + 4) === 5
$half = f::op("/", [1 => 2], 0);
$half(10); // (10 / 2) === 5
$teto = f::tuple("Teto Kasane", 31, "2008-04-01", "Baguette");
$ritsu = f::tuple("Ritsu Namine", 6, "2009-10-02", "Napa cabbage");
// index access
$teto[0]; // "Teto Kasane"
$teto[1]; // 31
$teto[2]; // "2008-04-01"
$teto[3]; // "Baguette"
// property access
$tetop = f::tuple("name", "Teto Kasane", "age", 31, "birthday", "2008-04-01", "item", "Baguette");
$ritsup = f::tuple("name", "Ritsu Namine", "age", 6, "birthday", "2009-10-02", "item", "Napa cabbage");
$tetop->pget("name"); // "Teto Kasane"
$tetop->pget("age"); // 31
$tetop->pget("birthday"); // "2008-04-01"
$tetop->pget("item"); // "Baguette"
$fib = f::fix(function ($fib) {
return function ($x) use ($fib) {
return ($x < 2) ? 1 : $fib($x - 1) + $fib($x -2);
};
});
$fib(6); // 13
// simple fibonacci function. But, very very slow.
$fib1 = function ($n) use (&$fib1) {
return ($n < 2) ? $n : $fib1($n - 1) + $fib1($n - 2);
};
// simple fibonacci function too. very fast!
$fib2 = f::memoize(function ($n) use (&$fib2) {
return ($n < 2) ? $n : $fib2($n - 1) + $fib2($n - 2);
}, [0, 1]);