Skip to content

Commit 6299cd4

Browse files
committed
Add flatMap
1 parent 14c900d commit 6299cd4

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

docs/Functions.md

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

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

55
* [_](#_)
66
* [add](#add)
@@ -39,6 +39,7 @@ Currently included functions (102):
3939
* [findLast](#findLast)
4040
* [findLastIndex](#findLastIndex)
4141
* [first](#first)
42+
* [flatMap](#flatMap)
4243
* [flatten](#flatten)
4344
* [flattenLevel](#flattenLevel)
4445
* [flip](#flip)
@@ -588,6 +589,18 @@ Phamda::first([]); // => false
588589
```
589590

590591

592+
<a name="flatMap"></a>
593+
### flatMap
594+
`array Phamda::flatMap(callable $function, array $list)`
595+
596+
Returns a list containing the flattened items created by applying the `function` to each item of the `list`.
597+
##### Examples
598+
```php
599+
$split = function ($string) { return str_split($string); };
600+
Phamda::flatMap($split, ['abc', 'de']); // => ['a', 'b', 'c', 'd', 'e']
601+
```
602+
603+
591604
<a name="flatten"></a>
592605
### flatten
593606
`array Phamda::flatten(array $list)`

src/Phamda.php

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

883+
/**
884+
* Returns a list containing the flattened items created by applying the `function` to each item of the `list`.
885+
*
886+
* ```php
887+
* $split = function ($string) { return str_split($string); };
888+
* Phamda::flatMap($split, ['abc', 'de']); // => ['a', 'b', 'c', 'd', 'e']
889+
* ```
890+
*
891+
* @param callable $function
892+
* @param array $list
893+
*
894+
* @return callable|array
895+
*/
896+
public static function flatMap($function = null, $list = null)
897+
{
898+
return static::curry2(function (callable $function, array $list) {
899+
return static::_flatten(static::_map($function, $list), false);
900+
}, func_get_args());
901+
}
902+
883903
/**
884904
* Returns an array that contains all the items on the `list`, with all arrays flattened.
885905
*

tests/BasicProvidersTrait.php

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

408+
public function getFlatMapData()
409+
{
410+
$duplicate = function ($x) { return [$x, $x]; };
411+
412+
return [
413+
[[2, 2, 3, 3, 6, 6], $duplicate, [2, 3, 6]],
414+
];
415+
}
416+
408417
public function getFlattenData()
409418
{
410419
return [

tests/BasicTest.php

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

418+
/**
419+
* @dataProvider getFlatMapData
420+
*/
421+
public function testFlatMap($expected, callable $function, array $list)
422+
{
423+
$this->assertSame($expected, Phamda::flatMap($function, $list), 'flatMap produces correct results.');
424+
$curried0 = Phamda::flatMap();
425+
$this->assertSame($expected, $curried0($function, $list), 'flatMap is curried correctly.');
426+
$curried1 = Phamda::flatMap($function);
427+
$this->assertSame($expected, $curried1($list), 'flatMap is curried correctly.');
428+
}
429+
418430
/**
419431
* @dataProvider getFlattenData
420432
*/

tests/FunctionExampleTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ public function testFindLastIndex()
188188
$this->assertSame(3, Phamda::findLastIndex($isPositive, [-5, 0, 15, 33, -2]));
189189
}
190190

191+
public function testFlatMap()
192+
{
193+
$split = function ($string) { return str_split($string); };
194+
$this->assertSame(['a', 'b', 'c', 'd', 'e'], Phamda::flatMap($split, ['abc', 'de']));
195+
}
196+
191197
public function testFlip()
192198
{
193199
$sub = function ($x, $y) { return $x - $y; };

0 commit comments

Comments
 (0)