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))); }
/** * @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)); } }
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); }
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()); }