reducing() public static method

Get a collector that applies the given reduction to the stream.
public static reducing ( mixed $identity, callable $reduction ) : phpstreams\Collector
$identity mixed
$reduction callable
return phpstreams\Collector
Example #1
0
 public function testReducing()
 {
     $instance = Collectors::reducing(1, function ($a, $b) {
         return $a * $b;
     });
     $instance->add("foo", 2);
     $instance->add("bar", 3);
     $instance->add("baz", 4);
     $this->assertEquals(24, $instance->get());
 }
Example #2
0
 /**
  * Get a collector that concatenates all the elements in the stream.
  *
  * @param string $delimiter [optional] A delimiter to insert between
  * elements. Defaults to the empty string.
  * @return Collector
  */
 public static function joining($delimiter = "")
 {
     $first = true;
     return Collectors::reducing("", function ($current, $element) use(&$first, $delimiter) {
         if (!$first) {
             $current .= $delimiter;
         } else {
             $first = false;
         }
         return $current . $element;
     });
 }