Skip to content

Commit 14c900d

Browse files
committed
Add flatten, flattenLevel
1 parent ddbca44 commit 14c900d

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

docs/Functions.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Phamda functions
22

3-
Currently included functions (100):
3+
Currently included functions (102):
44

55
* [_](#_)
66
* [add](#add)
@@ -39,6 +39,8 @@ Currently included functions (100):
3939
* [findLast](#findLast)
4040
* [findLastIndex](#findLastIndex)
4141
* [first](#first)
42+
* [flatten](#flatten)
43+
* [flattenLevel](#flattenLevel)
4244
* [flip](#flip)
4345
* [groupBy](#groupBy)
4446
* [gt](#gt)
@@ -586,6 +588,30 @@ Phamda::first([]); // => false
586588
```
587589

588590

591+
<a name="flatten"></a>
592+
### flatten
593+
`array Phamda::flatten(array $list)`
594+
595+
Returns an array that contains all the items on the `list`, with all arrays flattened.
596+
##### Examples
597+
```php
598+
Phamda::flatten([1, [2, 3], [4]]); // => [1, 2, 3, 4]
599+
Phamda::flatten([1, [2, [3]], [[4]]]); // => [1, 2, 3, 4]
600+
```
601+
602+
603+
<a name="flattenLevel"></a>
604+
### flattenLevel
605+
`array Phamda::flattenLevel(array $list)`
606+
607+
Returns an array that contains all the items on the `list`, with arrays on the first nesting level flattened.
608+
##### Examples
609+
```php
610+
Phamda::flattenLevel([1, [2, 3], [4]]); // => [1, 2, 3, 4]
611+
Phamda::flattenLevel([1, [2, [3]], [[4]]]); // => [1, 2, [3], [4]]
612+
```
613+
614+
589615
<a name="flip"></a>
590616
### flip
591617
`callable Phamda::flip(callable $function)`

src/CoreFunctionsTrait.php

+20
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ protected static function _filter(callable $predicate, $collection)
143143
return $result;
144144
}
145145

146+
/**
147+
* @param array $list
148+
* @param bool $recursive
149+
*
150+
* @return array
151+
*/
152+
protected static function _flatten(array $list, $recursive)
153+
{
154+
$result = [];
155+
foreach ($list as $item) {
156+
if (is_array($item)) {
157+
$result = array_merge($result, $recursive ? self::_flatten($item, $recursive) : $item);
158+
} else {
159+
$result[] = $item;
160+
}
161+
}
162+
163+
return $result;
164+
}
165+
146166
/**
147167
* @param callable $function
148168
* @param array|\Traversable|Collection $collection

src/Phamda.php

+38
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,44 @@ public static function first($collection = null)
880880
}, func_get_args());
881881
}
882882

883+
/**
884+
* Returns an array that contains all the items on the `list`, with all arrays flattened.
885+
*
886+
* ```php
887+
* Phamda::flatten([1, [2, 3], [4]]); // => [1, 2, 3, 4]
888+
* Phamda::flatten([1, [2, [3]], [[4]]]); // => [1, 2, 3, 4]
889+
* ```
890+
*
891+
* @param array $list
892+
*
893+
* @return callable|array
894+
*/
895+
public static function flatten($list = null)
896+
{
897+
return static::curry1(function (array $list) {
898+
return static::_flatten($list, true);
899+
}, func_get_args());
900+
}
901+
902+
/**
903+
* Returns an array that contains all the items on the `list`, with arrays on the first nesting level flattened.
904+
*
905+
* ```php
906+
* Phamda::flattenLevel([1, [2, 3], [4]]); // => [1, 2, 3, 4]
907+
* Phamda::flattenLevel([1, [2, [3]], [[4]]]); // => [1, 2, [3], [4]]
908+
* ```
909+
*
910+
* @param array $list
911+
*
912+
* @return callable|array
913+
*/
914+
public static function flattenLevel($list = null)
915+
{
916+
return static::curry1(function (array $list) {
917+
return static::_flatten($list, false);
918+
}, func_get_args());
919+
}
920+
883921
/**
884922
* Wraps the given function and returns a new function for which the order of the first two parameters is reversed.
885923
*

tests/BasicProvidersTrait.php

+16
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,22 @@ public function getFirstData()
405405
];
406406
}
407407

408+
public function getFlattenData()
409+
{
410+
return [
411+
[[1, 2, 3, 4], [1, [2, 3], [4]]],
412+
[[1, 2, 3, 4], [1, [2, [3]], [[4]]]],
413+
];
414+
}
415+
416+
public function getFlattenLevelData()
417+
{
418+
return [
419+
[[1, 2, 3, 4], [1, [2, 3], [4]]],
420+
[[1, 2, [3], [4]], [1, [2, [3]], [[4]]]],
421+
];
422+
}
423+
408424
public function getFlipData()
409425
{
410426
$subMany = function ($a, $b, $c = 0, $d = 0, $e = 0) {

tests/BasicTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,26 @@ public function testFirst($expected, $collection)
415415
$this->assertSame($expected, $curried0($collection), 'first is curried correctly.');
416416
}
417417

418+
/**
419+
* @dataProvider getFlattenData
420+
*/
421+
public function testFlatten($expected, array $list)
422+
{
423+
$this->assertSame($expected, Phamda::flatten($list), 'flatten produces correct results.');
424+
$curried0 = Phamda::flatten();
425+
$this->assertSame($expected, $curried0($list), 'flatten is curried correctly.');
426+
}
427+
428+
/**
429+
* @dataProvider getFlattenLevelData
430+
*/
431+
public function testFlattenLevel($expected, array $list)
432+
{
433+
$this->assertSame($expected, Phamda::flattenLevel($list), 'flattenLevel produces correct results.');
434+
$curried0 = Phamda::flattenLevel();
435+
$this->assertSame($expected, $curried0($list), 'flattenLevel is curried correctly.');
436+
}
437+
418438
/**
419439
* @dataProvider getFlipData
420440
*/

0 commit comments

Comments
 (0)