Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e73aa8
commit 400de52
Showing
13 changed files
with
675 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Dash\Generator; | ||
|
||
/** | ||
* @see Dash\filter() | ||
*/ | ||
// @codingStandardsIgnoreLine | ||
function filter($iterable, $predicate = 'Dash\identity') | ||
{ | ||
if (!is_callable($predicate)) { | ||
$predicate = call_user_func_array('Dash\matchesProperty', (array) $predicate); | ||
} | ||
|
||
$index = 0; | ||
$isIndexedArray = true; | ||
|
||
foreach ($iterable as $key => $value) { | ||
$isIndexedArray = $isIndexedArray && ($key === $index); | ||
|
||
if (call_user_func($predicate, $value, $key, $iterable)) { | ||
if ($isIndexedArray) { | ||
yield $value; | ||
} | ||
else { | ||
yield $key => $value; | ||
} | ||
} | ||
|
||
$index++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Dash\Generator; | ||
|
||
/** | ||
* @see Dash\take() | ||
*/ | ||
// @codingStandardsIgnoreLine | ||
function take($iterable, $count = 1) | ||
{ | ||
foreach ($iterable as $key => $value) { | ||
if ($count < 1) { | ||
break; | ||
} | ||
yield $key => $value; | ||
$count--; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
class GeneratorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider cases | ||
*/ | ||
public function testComposing($generator) | ||
{ | ||
$result = Dash\join( | ||
Dash\map( | ||
Dash\take( | ||
Dash\filter( | ||
$generator(), | ||
'Dash\isEven' | ||
), | ||
3 | ||
) | ||
), | ||
', ' | ||
); | ||
$this->assertSame('2, 4, 6', $result); | ||
} | ||
|
||
/** | ||
* @dataProvider cases | ||
*/ | ||
public function testChaining($generator) | ||
{ | ||
$result = Dash\chain($generator()) | ||
->filter('Dash\isEven') | ||
->take(3) | ||
->join(', ') | ||
->value(); | ||
|
||
$this->assertSame('2, 4, 6', $result); | ||
} | ||
|
||
public function cases() | ||
{ | ||
return [ | ||
'With a finite generator' => [ | ||
function () { | ||
foreach (range(1, 10) as $key => $value) { | ||
yield $key => $value; | ||
} | ||
}, | ||
], | ||
'With an infinite generator' => [ | ||
function () { | ||
$current = 1; | ||
while (true) { | ||
yield $current; | ||
$current++; | ||
} | ||
}, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.