PHP code example of phamda / phamda
1. Go to this page and download the library: Download phamda/phamda 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/ */
phamda / phamda example snippets
use Phamda\Phamda as P;
$isPositive = function ($x) { return $x > 0; };
$list = [5, 7, -3, 19, 0, 2];
$getPositives = P::filter($isPositive);
$getPositives($list) === [5, 7, 3 => 19, 5 => 2];
$replaceBad = P::curry('str_replace', 'bad', 'good');
$replaceBad('bad day') === 'good day';
$replaceBad('not bad') === 'not good';
$double = function ($x) { return $x * 2; };
$addFive = function ($x) { return $x + 5; };
$addFiveAndDouble = P::compose($double, $addFive);
$addFiveAndDouble(16) === 42;
// Equivalent to calling $double($addFive(16));
$doubleAndAddFive = P::pipe($double, $addFive);
$doubleAndAddFive(16) === 37;
$pow = function ($a, $b) { return $a ** $b; };
$powOf = P::flip($pow);
$pow(2, 8) === 256;
$powOf(2, 8) === 64;
$redact = P::twist('substr_replace')('REDACTED', 5);
$redact('foobarbaz') === 'foobaREDACTED';
$products = [
['category' => 'QDT', 'weight' => 65.8, 'price' => 293.5, 'number' => 15708],
['number' => 59391, 'price' => 366.64, 'category' => 'NVG', 'weight' => 15.5],
['category' => 'AWK', 'number' => 89634, 'price' => 341.92, 'weight' => 35],
['price' => 271.8, 'weight' => 5.3, 'number' => 38718, 'category' => 'ETW'],
['price' => 523.63, 'weight' => 67.9, 'number' => 75905, 'category' => 'YVM'],
['price' => 650.31, 'weight' => 3.9, 'category' => 'XPA', 'number' => 46289],
['category' => 'WGX', 'weight' => 75.5, 'number' => 26213, 'price' => 471.44],
['category' => 'KCF', 'price' => 581.85, 'weight' => 31.9, 'number' => 48160],
];
$formatPrice = P::flip('number_format')(2);
$process = P::pipe(
P::filter( // Only 'category' => 'NVG', 'price' => '366.64'],
['number' => 89634, 'category' => 'AWK', 'price' => '341.92'],
];