Exemplo n.º 1
0
 /**
  * @test
  */
 public function deepPartial()
 {
     $four = F\partial(function ($a, $b, $c, $d) {
         return $a + $b + $c + $d;
     });
     $x = $four(1);
     $y = $x(2);
     $z = $y(3);
     $this->assertEquals(10, $z(4));
     $this->assertEquals(10, $four(1, 2, 3, 4));
     $x = F\partial(function ($a, $b, $c) {
         return $a($b($c));
     });
     $y = $x(function ($x) {
         return $x + 1;
     });
     $z = $y(function ($x) {
         return $x * 2;
     });
     $this->assertEquals(21, $z(10));
 }
Exemplo n.º 2
0
<?php

use Sergiors\Functional as F;
require_once __DIR__ . '/../vendor/autoload.php';
$join = F\pipe(F\flatten, F\partial('implode', ', '));
$mapCountry = F\map(function ($x) {
    return $x['country'];
});
$countries = F\pipe($mapCountry, $join)->strtoupper();
// or `->pipe('strtoupper')`
$mapCity = F\map(function ($x) {
    return $x['cities'];
});
$cities = F\pipe($mapCity)->pipe($join);
$xs = [['country' => 'Brazil', 'cities' => ['Florianópolis', 'Rio de Janeiro', 'Porto Alegre']], ['country' => 'USA', 'cities' => ['Baltimore', 'San Diego']], ['country' => 'China', 'cities' => ['Macau', 'Hong Kong']]];
assert('BRAZIL, USA, CHINA' === $countries($xs));
assert('Florianópolis, Rio de Janeiro, Porto Alegre, Baltimore, San Diego, Macau, Hong Kong' === $cities($xs));