예제 #1
0
 public function testLetsDoCoolThingsSuchAsMapReduce()
 {
     $arrayOfPhrases = array('first second third', 'first second', 'fourth second fourth', 'first second second', 'third second third');
     $phrasesStream = new S\Stream($arrayOfPhrases);
     $computedArray = $phrasesStream->map(function ($line) {
         return array_count_values(explode(' ', $line));
     })->reduce(array(), function ($acc, $next) {
         foreach ($next as $word => $count) {
             if (isset($acc[$word])) {
                 $acc[$word] += $count;
             } else {
                 $acc[$word] = $count;
             }
         }
         return $acc;
     });
     $this->assertEquals(3, $computedArray['first']);
     $this->assertEquals(6, $computedArray['second']);
     $this->assertEquals(3, $computedArray['third']);
     $this->assertEquals(2, $computedArray['fourth']);
 }