From 24ba1caf02094acd2c69a0d39d397f27bb2d3771 Mon Sep 17 00:00:00 2001 From: Pekka Laiho Date: Sat, 6 Jun 2020 08:42:38 +0700 Subject: [PATCH] add apply function --- src/Lib/Collections.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Lib/Collections.php b/src/Lib/Collections.php index 8fca970..9614b28 100644 --- a/src/Lib/Collections.php +++ b/src/Lib/Collections.php @@ -94,6 +94,29 @@ class Collections implements ILib // Manipulate list + $env->set('apply', new CoreFunc('apply', 'Apply the first argument (function) using second argument (sequence) as arguments.', 2, -1, + function (...$args) { + $fn = $args[0]; + $seq = $args[count($args) - 1]; + + if (!($fn instanceof Func)) { + throw new MadLispException('first argument to apply is not function'); + } elseif (!($seq instanceof Seq)) { + throw new MadLispException('last argument to apply is not sequence'); + } + + $args2 = []; + for ($i = 1; $i < count($args) - 1; $i++) { + $args2[] = $args[$i]; + } + foreach ($seq->getData() as $a) { + $args2[] = $a; + } + + return $fn->call($args2); + } + )); + $env->set('chunk', new CoreFunc('chunk', 'Divide first argument (sequence) into new sequences with length of second argument (int).', 2, 2, function (Seq $a, int $len) { $chunks = array_chunk($a->getData(), $len);