Пример #1
0
 public function __invoke() : View
 {
     $extractShorName = function ($item) {
         return $item->state->getShortName();
     };
     $allItems = Item::all();
     $newItems = P::pipe(P::filter(P::pipe($extractShorName, P::eq('new'))), 'P::size');
     return view('main')->with('items', $allItems)->with('remaining_item_count', $newItems($allItems));
 }
Пример #2
0
 public function testPipe()
 {
     $triple = function ($x) {
         return $x * 3;
     };
     $double = function ($x) {
         return $x * 2;
     };
     $square = function ($x) {
         return $x * $x;
     };
     $pipe = P::pipe($square, $double, $triple);
     $this->assertEquals(150, $pipe(5));
 }
Пример #3
0
//-> NAN
println(apply($safeDivide)(5, 0));
require_once '../../vendor/autoload.php';
require_once '../ch08/model/User.php';
require_once '../ch08/model/Account.php';
use Model\Account;
use Model\User;
use Rx\Observable;
use Rx\Observer;
// SELECT firstname
// FROM users
// WHERE firstname IS NOT NULL
// ORDER BY firstname DESC
// LIMIT 1;
print_r(P::pipe('\\Model\\User::query', P::map(P::prop('firstname')), P::filter(function ($n) {
    return !empty($n);
}), 'P::reverse', P::take(1))());
P::compose(P::take(1), 'P::reverse', P::filter(function ($n) {
    return !empty($n);
}), P::map(P::prop('firstname')), '\\Model\\User::query')();
// PHP Warning:  f() expects
// at least 2 parameters, 1 given
// $format = P::compose('addExclamation', $repeat(2), 'strtoupper');
// $format('Hello World'); //-> HELLO WORLD HELLO WORLD!
$applyTax = P::curry2(function ($rate, $amount) {
    return $amount + $amount * ($rate / 100);
});
$applyTax(6.0);
//-> Closure
$applyTax(6.0, 100);
//-> 106