Skip to content

Conversation

@win0err
Copy link
Contributor

@win0err win0err commented Jun 7, 2019

Converge

Accepts a converging function and a list of branching functions and returns a new function. When invoked, this new function is applied to some arguments, and each branching function is applied to those same arguments. The results of each branching function are passed as arguments to the converging function to produce the return value.

<?php
converge('intdiv', ['max', 'min'])([2, 3, 4, 5, 6]); // 3

JS ramda analog

@win0err win0err force-pushed the feature/converge-function branch from bd50f06 to 1c76947 Compare June 7, 2019 19:10
@Erikvv
Copy link
Contributor

Erikvv commented Jun 7, 2019

What is the reason to do this instead of equivalent code which is also functional:

function middle(array $data) {
    return intdiv(max($data), min($data));
}

middle([2, 3, 4, 5, 6]);

Converge would be useful if you don't know which functions to apply beforehand but I can't think of such case.

Do you have a good usecase?

@win0err
Copy link
Contributor Author

win0err commented Jun 7, 2019

What is the reason to do this instead of equivalent code which is also functional:

function middle(array $data) {
    return intdiv(max($data), min($data));
}

middle([2, 3, 4, 5, 6]);

Converge would be useful if you don't know which functions to apply beforehand but I can't think of such case.

Do you have a good usecase?

<?php
// sma() function is taken from here: 
// https://github.com/markrogoyski/math-php/blob/master/src/Statistics/Average.php#L650
$sma = curry_n(2, flip('sma'));

$processingFunction = fn (...$args) => array_map(curry('implode')(''), $args);
$getChart = converge(
    $processingFunction,
    [$sma(1), $sma(2), $sma(4), $sma(8)]
);
// vs.
$getChart2 = function ($data) use ($sma) {
    return [
        implode('', $sma(1)($data)),
        implode('', $sma(2)($data)),
        implode('', $sma(4)($data)),
        implode('', $sma(8)($data)),
    ];
};

$chart = $getChart([1, 2, 3, 4, 5, 6, 7, 8, 9]);

Pros of converge is a more functional approach. It's easy to add another function like $sma(42) to the list of branching functions.

Copy link
Owner

@lstrojny lstrojny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good! Can you mention the function in the docs as well?

return function (...$values) use ($convergingFunction, $branchingFunctions) {
$result = [];

foreach ($branchingFunctions as $f) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as $branchingFunction for clarity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

'strtolower',
]
);
$this->assertEquals('FUNCTIONAL PROGRAMMINGfunctional programming', $strangeFunction('Functional Programming'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertSame()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor Author

@win0err win0err left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@lstrojny
Copy link
Owner

Awesome, thank you!

@lstrojny lstrojny merged commit 7bab942 into lstrojny:master Jun 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants