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)); }
public function testFilter() { $list1 = function () { (yield json_decode('{"a": 1, "b":1}')); (yield json_decode('{"a": 2, "b":2}')); (yield json_decode('{"a": 3, "b":3}')); }; $list2 = [json_decode('{"a": 1, "b":1}'), json_decode('{"a": 2, "b":2}'), json_decode('{"a": 3, "b":3}')]; $aIsTwo = function ($item) { return $item->a == 2 ? TRUE : FALSE; }; $this->assertEquals([json_decode('{"a": 2, "b":2}')], P::toArray(P::filter($aIsTwo, $list1()))); $this->assertEquals([json_decode('{"a": 2, "b":2}')], P::toArray(P::filter($aIsTwo, $list2))); $list3 = [1, 2]; $valueIsOne = function ($v) { return $v === 1 ? TRUE : FALSE; }; $this->assertEquals(1, P::head(P::filter($valueIsOne, $list3))); $list4 = ['a' => 1, 'b' => 2]; $valueIsOne = function ($v, $key) { return $v === 1 ? TRUE : FALSE; }; $this->assertEquals(['a' => 1], P::head(P::filter($valueIsOne, $list4))); }
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 $applyTax(6.0)(100); //-> 106 // $total = P::compose($currency('USD'), $applyTax(6.0), 'P::sum');