-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added flat_map function * Fixed flat_map tests * Added flat_map function * A little fix in flat_map readme * Modifed signature of flat_map and readme description * Removed an space * Modified description of flat_map example * Added flat_map to index * fixed flat_map comment style
- Loading branch information
1 parent
43b6678
commit f25a978
Showing
5 changed files
with
92 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# flat_map | ||
|
||
## Description | ||
Returns an array containing the results of applying a given function to the items of a collection and flattening the results. | ||
|
||
## Parameters | ||
|
||
<dl> | ||
<dt>fn</dt> | ||
<dd>Function to apply to every item in the collection. Must return a array|Traversable|Generator</dd> | ||
|
||
<dt>coll</dt> | ||
<dd>Collection of 1-n dimensions</dd> | ||
</dl> | ||
|
||
## Examples | ||
|
||
Create a collection of ranges and flatten the results: | ||
```php | ||
<?php | ||
|
||
use function Lambdish\Phunctional\flat_map; | ||
|
||
$range = [2, 3, 4]; | ||
|
||
$naturalRange = function ($value): array { | ||
return range(1, $value); | ||
}; | ||
|
||
return flat_map($naturalRange, $range); | ||
|
||
// => [1, 2, 1, 2, 3, 1, 2, 3, 4] | ||
``` |
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,25 @@ | ||
<?php | ||
|
||
namespace Lambdish\Phunctional; | ||
|
||
use Generator; | ||
use Traversable; | ||
|
||
/** | ||
* Returns an array containing the results of applying $fn to the items of the $coll | ||
* and flattening the results. | ||
* | ||
* Function $fn should accept the value of the item as the first argument | ||
* and optionally the key of the item as the second argument. | ||
* | ||
* @param callable $fn function with signature Closure(mixed): array|Traversable|Generator | ||
* @param array|Traversable|Generator $coll collection of values | ||
* | ||
* @return array | ||
* @since 1.0.8 Added flat_map function | ||
* | ||
*/ | ||
function flat_map(callable $fn, $coll) | ||
{ | ||
return flatten(map($fn, $coll)); | ||
} |
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 Lambdish\Phunctional\Tests; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use function Lambdish\Phunctional\flat_map; | ||
|
||
class FlatMapTest extends PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** @test */ | ||
public function it_should_apply_and_then_flatten() | ||
{ | ||
$actual = [2, 3, 4]; | ||
$expected = [1, 2, 1, 2, 3, 1, 2, 3, 4]; | ||
|
||
$naturalRange = function ($value) { | ||
return range(1, $value); | ||
}; | ||
|
||
$this->assertSame($expected, flat_map($naturalRange, $actual)); | ||
} | ||
|
||
/** @test */ | ||
public function it_should_allow_receive_the_key_in_the_function_to_apply() | ||
{ | ||
$actual = [4 => 1, 9 => 5]; | ||
$expected = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
|
||
$this->assertSame($expected, flat_map('range', $actual)); | ||
} | ||
} |