-
-
Notifications
You must be signed in to change notification settings - Fork 209
Add converge function #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
bd50f06 to
1c76947
Compare
|
What is the reason to do this instead of equivalent code which is also functional: 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 |
lstrojny
left a comment
There was a problem hiding this 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?
src/Functional/Converge.php
Outdated
| return function (...$values) use ($convergingFunction, $branchingFunctions) { | ||
| $result = []; | ||
|
|
||
| foreach ($branchingFunctions as $f) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as $branchingFunction for clarity?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
tests/Functional/ConvergeTest.php
Outdated
| 'strtolower', | ||
| ] | ||
| ); | ||
| $this->assertEquals('FUNCTIONAL PROGRAMMINGfunctional programming', $strangeFunction('Functional Programming')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertSame()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
win0err
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
|
Awesome, thank you! |
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.
JS ramda analog