本文整理汇总了PHP中Sequence::make方法的典型用法代码示例。如果您正苦于以下问题:PHP Sequence::make方法的具体用法?PHP Sequence::make怎么用?PHP Sequence::make使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sequence
的用法示例。
在下文中一共展示了Sequence::make方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testInterviewQuestionA
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)));
}
示例2: getChildren
/**
* @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));
}
}
示例3: testFnTrim
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);
}
示例4: testFnAvg
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);
}
示例5: testBinding
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);
}
示例6: test_to_fn
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());
}