For compatibility reasons, the returned collectors are actuall classes, rather than anonymous classes. This is an implementation detail, and should not be relied upon. The implementing classes may be removed at any point.
Author: Bert Peters (bert.ljpeters@gmail.com)
 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());
 }
Exemple #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;
     });
 }