Skip to content

Commit be7ca0a

Browse files
committed
Add Collection.flatMapDeep function
1 parent 825c8c8 commit be7ca0a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/Collection/flatMapDeep.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
namespace _;
13+
14+
use function _\internal\baseFlatten;
15+
16+
/**
17+
* This method is like `flatMap` except that it recursively flattens the
18+
* mapped results.
19+
*
20+
* @category Collection
21+
*
22+
* @param iterable collection The collection to iterate over.
23+
* @param callable $iteratee The function invoked per iteration.
24+
*
25+
* @return array Returns the new flattened array.
26+
* @example
27+
* <code>
28+
* function duplicate($n) {
29+
* return [[[$n, $n]]];
30+
* }
31+
*
32+
* flatMapDeep([1, 2], 'duplicate');
33+
* // => [1, 1, 2, 2]
34+
* </code>
35+
*/
36+
function flatMapDeep(iterable $collection, callable $iteratee = null): array
37+
{
38+
return baseFlatten(map($collection, $iteratee), \PHP_INT_MAX);
39+
}

tests/Collection/FlatMapDeepTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SolidWorx Lodash-PHP project.
7+
*
8+
* @author Pierre du Plessis <open-source@solidworx.co>
9+
* @copyright Copyright (c) 2017
10+
*/
11+
12+
use PHPUnit\Framework\TestCase;
13+
use function _\flatMapDeep;
14+
15+
class FlatMapDeepTest extends TestCase
16+
{
17+
public function testFlatMapDeep()
18+
{
19+
$duplicate = function ($n) {
20+
return [[[$n, $n]]];
21+
};
22+
23+
$this->assertSame([1, 1, 2, 2], flatMapDeep([1, 2], $duplicate));
24+
}
25+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy