Exemplo n.º 1
0
 public function testInterviewQuestionA()
 {
     /*
        The sum of all natural numbers below 10 that are multiples of 3 or 5 are 23 (3 + 5 + 6 + 9)
        Write a php script that will find the sum of all the multiples of 3 or 5 below 1000. The script
        should run from command line and put the result on screen. We will judge this task based on
        simplicity, efficiency and cleverness of the code.
     */
     $limit = 1000;
     $values = range(0, $limit);
     $a = 3;
     $b = 5;
     $fnFilterMaker = function ($a, $b) {
         return function ($v) use($a, $b) {
             return $v % $a == 0 || $v % $b == 0;
         };
     };
     // test: sum of multiples of 3 or 5 below 10 is 23 (3 + 5 + 6 + 9)
     $this->assertTrue(Sequence::make(range(0, 9))->filter($fnFilterMaker(3, 5))->reduce(0, FnGen::fnSum()) == 23);
     $filteredValues = Sequence::make($values)->filter($fnFilterMaker($a, $b))->to_a();
     $this->assertArrayHasKey($a, $filteredValues);
     $this->assertArrayHasKey($b, $filteredValues);
     $this->assertArrayNotHasKey($a * $b + 1, $filteredValues);
     $valuesOnly = array_values($filteredValues);
     $subsetA = range(0, $limit, $a);
     $subsetB = range(0, $limit, $b);
     $this->assertTrue($subsetA == array_values(array_intersect($valuesOnly, $subsetA)));
     $this->assertTrue($subsetB == array_values(array_intersect($valuesOnly, $subsetB)));
     $this->assertTrue(!count(array_diff($valuesOnly, $subsetA, $subsetB)));
 }
Exemplo n.º 2
0
 /**
  * @return TraverseSequence|MappedSequence
  */
 public function getChildren()
 {
     $x = $this->current();
     if ($this->canGoDeeper()) {
         return TraverseSequence::make($x, $this->key(), $this->pathSeparator)->setMaxDepth($this->depth - 1);
     } else {
         return IterationTraits::map(Sequence::make($x), FnGen::fnIdentity(), FnString::fnAddPrefix($this->key() . $this->pathSeparator));
     }
 }
Exemplo n.º 3
0
 public function testFnTrim()
 {
     $fn = FnString::fnTrim();
     $this->assertEquals('test', $fn('  test '));
     $array = array(' 352', '354 ', '333', ' 12 34 ', "\n  Cool Stuff  \n", "\r", "CRLF\r\n");
     $expectedTrimmedArray = array('352', '354', '333', '12 34', 'Cool Stuff', '', 'CRLF');
     $this->assertNotEquals($array, $expectedTrimmedArray);
     $trimmedArray = Sequence::make($array)->map(FnString::fnTrim())->to_a();
     $this->assertEquals($trimmedArray, $expectedTrimmedArray);
 }
Exemplo n.º 4
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);
 }
Exemplo n.º 5
0
 public function testBinding()
 {
     $count = 0;
     $data = array(1, 2, 3, 4, 5, 6);
     $fnGetIterator = function () use(&$count, $data) {
         $count += 1;
         return new \ArrayIterator($data);
     };
     $onDemandIterator = new OnDemandIterator($fnGetIterator);
     $this->assertEquals(0, $count);
     $seq = Sequence::make($onDemandIterator);
     // Assert that the iterator wasn't called
     $this->assertEquals(0, $count);
     $result = $seq->to_a();
     // Assert that the iterator was called just once
     $this->assertEquals(1, $count);
     // Make sure the value are as expected
     $this->assertEquals($data, $result);
 }
Exemplo n.º 6
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());
 }