Skip to content

Commit ddbca44

Browse files
committed
Add concat
1 parent 0d66cbb commit ddbca44

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-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 (99):
3+
Currently included functions (100):
44

55
* [_](#_)
66
* [add](#add)
@@ -19,6 +19,7 @@ Currently included functions (99):
1919
* [clone_](#clone_)
2020
* [comparator](#comparator)
2121
* [compose](#compose)
22+
* [concat](#concat)
2223
* [construct](#construct)
2324
* [constructN](#constructN)
2425
* [contains](#contains)
@@ -332,6 +333,18 @@ $upperHello('world'); // => 'HELLO WORLD'
332333
```
333334

334335

336+
<a name="concat"></a>
337+
### concat
338+
`string Phamda::concat(string $a, string $b)`
339+
340+
Returns a string concatenated of `a` and `b`.
341+
##### Examples
342+
```php
343+
Phamda::concat('ab', 'cd'); // => 'abcd'
344+
Phamda::concat('abc', ''); // => 'abc'
345+
```
346+
347+
335348
<a name="construct"></a>
336349
### construct
337350
`object Phamda::construct(string $class, mixed ... $initialArguments)`

src/Phamda.php

+20
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,26 @@ public static function compose(... $functions)
422422
return Phamda::pipe(...array_reverse($functions));
423423
}
424424

425+
/**
426+
* Returns a string concatenated of `a` and `b`.
427+
*
428+
* ```php
429+
* Phamda::concat('ab', 'cd'); // => 'abcd'
430+
* Phamda::concat('abc', ''); // => 'abc'
431+
* ```
432+
*
433+
* @param string $a
434+
* @param string $b
435+
*
436+
* @return callable|string
437+
*/
438+
public static function concat($a = null, $b = null)
439+
{
440+
return static::curry2(function ($a, $b) {
441+
return $a . $b;
442+
}, func_get_args());
443+
}
444+
425445
/**
426446
* Wraps the constructor of the given class to a function.
427447
*

tests/BasicProvidersTrait.php

+8
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ public function getComposeData()
181181
];
182182
}
183183

184+
public function getConcatData()
185+
{
186+
return [
187+
['abcd', 'ab', 'cd'],
188+
['abc', 'abc', ''],
189+
];
190+
}
191+
184192
public function getConstructData()
185193
{
186194
return [

tests/BasicTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ public function testCompose($expected, array $functions, ... $arguments)
201201
$this->assertSame($expected, $main0(...$arguments), 'compose produces correct results.');
202202
}
203203

204+
/**
205+
* @dataProvider getConcatData
206+
*/
207+
public function testConcat($expected, $a, $b)
208+
{
209+
$this->assertSame($expected, Phamda::concat($a, $b), 'concat produces correct results.');
210+
$curried0 = Phamda::concat();
211+
$this->assertSame($expected, $curried0($a, $b), 'concat is curried correctly.');
212+
$curried1 = Phamda::concat($a);
213+
$this->assertSame($expected, $curried1($b), 'concat is curried correctly.');
214+
}
215+
204216
/**
205217
* @dataProvider getContainsData
206218
*/

0 commit comments

Comments
 (0)