Example #1
0
 public function testFnAvg()
 {
     $range = range(1, 10);
     $this->assertEquals(5.5, Sequence::make($range)->reduce(0, FnGen::fnAvg()));
     $fruit = array(array('name' => 'apple', 'count' => 1), array('name' => 'orange', 'count' => 5), array('name' => 'apple', 'count' => 3), array('name' => 'banana', 'count' => 9), array('name' => 'Out of Stock'), array('name' => 'orange', 'count' => 5));
     $counts = Sequence::make($fruit)->map(FnGen::fnPluck('count'))->filter(FnGen::fnKeepIsSet())->to_a();
     $avg = array_sum($counts) / count($counts);
     $avg2 = Sequence::make($fruit)->reduce(0, FnReduce::fnAvg(FnGen::fnPluck('count')));
     $this->assertEquals($avg, $avg2);
 }
Example #2
0
 public function test_to_fn()
 {
     $field = 'name';
     // Make a function that will pluck the names from the set of fruit.
     $fnMap = FnSequence::make()->map(FnGen::fnPluck($field))->to_fn();
     $this->assertEquals(Sequence::make(TestData::$fruit)->map(FnGen::fnPluck($field))->to_a(), Sequence::make($fnMap(TestData::$fruit))->to_a());
     // Nest them.
     $fnLimit = FnSequence::make($fnMap)->limit(1)->to_fn();
     $this->assertEquals(Sequence::make(TestData::$fruit)->map(FnGen::fnPluck($field))->limit(1)->to_a(), Sequence::make($fnLimit(TestData::$fruit))->to_a());
     // Test the Not equal to make sure we are not just getting back a bunch of nulls.
     $this->assertNotEquals(Sequence::make(TestData::$fruit)->map(FnGen::fnPluck($field))->limit(2)->to_a(), Sequence::make($fnLimit(TestData::$fruit))->to_a());
 }