コード例 #1
0
ファイル: pramda.php プロジェクト: kapolos/pramda
 public function testTake()
 {
     $list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
     $this->assertEquals([1, 2, 3, 4, 5], P::toArray(P::take(5, $list)));
     $a = function () {
         for ($i = 1; $i < 10; $i++) {
             (yield $i);
         }
     };
     $this->assertEquals([1, 2, 3, 4, 5], P::toArray(P::take(5, $a())));
     $listAssoc = ['a' => 1, 'b' => '2', 'c' => 3];
     $this->assertEquals(['a' => 1, 'b' => '2'], P::toArray(P::take(2, $listAssoc)));
 }
コード例 #2
0
ファイル: ch04.php プロジェクト: luijar/fp-php
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
$applyTax(6.0)(100);
//-> 106