Skip to content

Commit bf439a9

Browse files
jasonmmlstrojny
authored andcommitted
Converge docs (#205)
* Write docs for `converge`. Closes #188. * Fix a few typos in docs.
1 parent d00c9e8 commit bf439a9

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

docs/functional-php.md

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [partial_left() & partial_right()](#partial_left--partial_right)
2424
- [partial_any()](#partial_any)
2525
- [partial_method()](#partial_method)
26+
- [converge()](#converge)
2627
- [Currying](#currying)
2728
- [curry()](#curry)
2829
- [curry_n()](#curry_n)
@@ -411,6 +412,32 @@ $users = [new User(), new User()];
411412
$registeredUsers = select($users, partial_method('isRegistered'));
412413
```
413414

415+
## converge()
416+
417+
``callable Functional\converge(callable $convergingFunction, callable[] branchingFunctions)``
418+
419+
`converge` accepts a converging function and a list of branching functions and returns a new function.
420+
421+
The returned function takes a variable number of arguments.
422+
423+
The _converging function_ should take the same number of arguments as there are branching functions.
424+
425+
Each _branching function_ should take the same number of arguments as the number of arguments passed in to the returned function.
426+
427+
```php
428+
use function Functional\converge;
429+
430+
function div($dividend, $divisor) {
431+
return $dividend / $divisor;
432+
}
433+
434+
$average = converge('div', ['array_sum', 'count']);
435+
$average([1, 2, 3, 4]); // -> 2.5
436+
```
437+
438+
The returned function, in the above example it is named `$average`, passes each of its arguments to each branching function. `$average` then takes the return values of all the branching functions and passes each one as an argument to the converging function. The return value of the converging function is the return value of `$average`.
439+
440+
414441
# Currying
415442

416443
Currying is similar to and often confused with partial application. But instead of binding parameters to some value and returning a new function, a curried function will take one parameter on each call and return a new function until all parameters are bound.
@@ -462,7 +489,7 @@ $curriedAdd = curry('add', true);
462489
$curriedAddWithOptional = curry('add', false);
463490
```
464491

465-
Starting with PHP7 and the implementation of the ["Uniform variable syntax"](https://wiki.php.net/rfc/uniform_variable_syntax), you can greatly simpliy the usage of curried functions.
492+
Starting with PHP7 and the implementation of the ["Uniform variable syntax"](https://wiki.php.net/rfc/uniform_variable_syntax), you can greatly simplify the usage of curried functions.
466493

467494
```php
468495
use function Functional\curry;
@@ -479,10 +506,10 @@ _Note, that you cannot use `curry` on a flipped function. `curry` uses reflectio
479506

480507
## curry_n()
481508

482-
`curry` uses reflection to determine the number of arguments, which can be slow depdening on your requirements. Also, you might want to curry only the first parameters, or your function expects a variable number of parameters. In all cases, you can use `curry_n` instead.
509+
`curry` uses reflection to determine the number of arguments, which can be slow depending on your requirements. Also, you might want to curry only the first parameters, or your function expects a variable number of parameters. In all cases, you can use `curry_n` instead.
483510

484511
```php
485-
use function Functional\curry;
512+
use function Functional\curry_n;
486513

487514
function add($a, $b, $c, $d) {
488515
return $a + $b + $c + $d;

0 commit comments

Comments
 (0)