From 4a0637462e996807d834022a0e2cbe368c28a676 Mon Sep 17 00:00:00 2001 From: tsnorri Date: Mon, 28 May 2018 23:30:40 +0300 Subject: [PATCH 1/3] Add unfold --- LambdaExtension.php | 22 +++++++++++++++++++++- README.md | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/LambdaExtension.php b/LambdaExtension.php index a62b05f..2454703 100644 --- a/LambdaExtension.php +++ b/LambdaExtension.php @@ -67,6 +67,7 @@ public function getFilters() new \Twig_SimpleFilter('group_by', '\DPolac\TwigLambda\LambdaExtension::groupBy'), new \Twig_SimpleFilter('sort_by', '\DPolac\TwigLambda\LambdaExtension::sortBy'), new \Twig_SimpleFilter('count_by', '\DPolac\TwigLambda\LambdaExtension::countBy'), + new \Twig_SimpleFilter('unfold', '\DPolac\TwigLambda\LambdaExtension::unfold'), ]; } @@ -292,9 +293,28 @@ public static function call($callback, array $args = []) } return call_user_func_array($callback, $args); } + + public static function unfold($state, $callback, array $unfolded = []) + { + if (!is_callable($callback)) { + throw new \InvalidArgumentException('First argument must be callable.'); + } + + while (true) + { + $retval = $callback($state); + if (is_null($retval)) + break; + + list ($newVal, $state) = $retval; + $unfolded[] = $newVal; + } + + return $unfolded; + } public function getName() { return 'dpolac_lambda_extension'; } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 618c537..68c9e94 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,22 @@ Returns array of elements that passes a test specified by lambda. ---------------------------------------------------------------- + +### |unfold +**Signature:** `state|unfold(lambda[, retval = []])` + +Given a lambda that returns an array `[state, value]` or `null`, repeatedly +calls the function until it returns `null` passing the new `state` on each +call. Returns an array of the accumulated values. + + +```twig +{% do [1, 1]|unfold(=> _[1] > 1000 ? null : [ _[0] + _[1], [ _[1], _[0] + _[1]]], [1, 1]) %} +{# Calculates the Fibonacci numbers up to and including 1597. #} +``` + +---------------------------------------------------------------- + ### |unique_by **Signature:** `array|unique_by(lambda|'==='|'==')` From 2a4ad804ce27f49add601313dd8914217293327d Mon Sep 17 00:00:00 2001 From: Tuukka Norri Date: Tue, 29 May 2018 01:26:07 +0300 Subject: [PATCH 2/3] Fix argument order for unfold --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 68c9e94..48f7fb9 100644 --- a/README.md +++ b/README.md @@ -125,8 +125,8 @@ Returns array of elements that passes a test specified by lambda. ### |unfold **Signature:** `state|unfold(lambda[, retval = []])` -Given a lambda that returns an array `[state, value]` or `null`, repeatedly -calls the function until it returns `null` passing the new `state` on each +Given a lambda that returns an array `[value, state]` or `null`, repeatedly +calls the function until it returns `null`, passing the new `state` on each call. Returns an array of the accumulated values. From 95fc5ae1e8392ec26e9ce708287848e24636cdc7 Mon Sep 17 00:00:00 2001 From: Justin Cherniak Date: Wed, 12 Jun 2019 23:25:49 -0700 Subject: [PATCH 3/3] Fix #11 - Change operator to ==> to not conflict with built in => operator in Twig 2.10 --- LambdaExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LambdaExtension.php b/LambdaExtension.php index a62b05f..deb440e 100644 --- a/LambdaExtension.php +++ b/LambdaExtension.php @@ -19,13 +19,13 @@ public function getOperators() { return [ [ - '=>' => [ + '==>' => [ 'precedence' => 0, 'class' => '\DPolac\TwigLambda\NodeExpression\SimpleLambda' ], ], [ - '=>' => [ + '==>' => [ 'precedence' => 0, 'class' => '\DPolac\TwigLambda\NodeExpression\LambdaWithArguments', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT @@ -297,4 +297,4 @@ public function getName() { return 'dpolac_lambda_extension'; } -} \ No newline at end of file +}