]> granicus.if.org Git - icinga2/commitdiff
Change how Array#reduce works
authorGunnar Beutner <gunnar.beutner@netways.de>
Fri, 29 Jul 2016 09:09:46 +0000 (11:09 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Fri, 29 Jul 2016 09:09:46 +0000 (11:09 +0200)
refs #12247

doc/19-library-reference.md
lib/base/array-script.cpp

index 99664fb03c4defc1691b7efcb4553ed80f565425..83efd8c4eb8343bee3a91cef178dd1c5cd781112 100644 (file)
@@ -708,8 +708,8 @@ Signature:
     function reduce(func);
 
 Reduces the elements of the array into a single value by calling the provided
-function `func` as `func(a, b)` repeatedly where `a` is the previous result of
-function call (null initially) and `b` is an element of the array.
+function `func` as `func(a, b)` repeatedly where `a` and `b` are elements of the array
+or results from previous function calls.
 
 ### <a id="array-filter"> Array#filter
 
index c0482312a1fa14b5a06174f0cac37e3bbfa8c988..f5924791d3e764d2b50b0e94d5ecbc3542516982 100644 (file)
@@ -172,14 +172,17 @@ static Value ArrayReduce(const Function::Ptr& function)
        if (vframe->Sandboxed && !function->IsSideEffectFree())
                BOOST_THROW_EXCEPTION(ScriptError("Reduce function must be side-effect free."));
 
-       Value result;
+       if (self->GetLength() == 0)
+               return Empty;
+
+       Value result = self->Get(0);
 
        ObjectLock olock(self);
-       BOOST_FOREACH(const Value& item, self) {
+       for (size_t i = 1; i < self->GetLength(); i++) {
                ScriptFrame uframe;
                std::vector<Value> args;
                args.push_back(result);
-               args.push_back(item);
+               args.push_back(self->Get(i));
                result = function->Invoke(args);
        }